Skip to content

Commit

Permalink
send role to app from api b00tc4mp#485
Browse files Browse the repository at this point in the history
  • Loading branch information
juditta99 committed Jun 17, 2024
1 parent 9c68718 commit 3b54c1e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export default (req, res) => {
try {
logic
.authenticateUser(email, password)
.then((userId) => {
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET, {
.then((user) => {
const token = jwt.sign({ sub: user.id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXP,
})
res.json({ token })
const role = user.role
res.json({ token, role })
res.send()
})
.catch((error) => {
Expand Down
3 changes: 2 additions & 1 deletion staff/judy-grayland/project/api/logic/authenticateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function authenticateUser(email, password) {
if (!match) {
throw new CredentialsError('wrong password')
}
return user.id

return user
})
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function authenticateUser(email, password) {

res
.json()
.then(({ token }) => {
.then(({ token, role }) => {
const payloadB64 = token.slice(
token.indexOf('.') + 1,
token.lastIndexOf('.')
Expand All @@ -57,6 +57,7 @@ function authenticateUser(email, password) {

context.sessionUserId = userId
context.token = token
context.role = role
})
.catch((error) => {
throw new SystemError(error)
Expand Down
12 changes: 12 additions & 0 deletions staff/judy-grayland/project/app/src/logic/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ const context = {
get token() {
return sessionStorage.token ? sessionStorage.token : null
},

set role(role) {
if (role) {
sessionStorage.role = role
} else {
delete sessionStorage.role
}
},

get role() {
return sessionStorage.role ? sessionStorage.role : null
},
}

export default context

0 comments on commit 3b54c1e

Please sign in to comment.