Skip to content

Commit

Permalink
add functionality of role retrieval, to use in whole app b00tc4mp#425
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Feb 26, 2024
1 parent 8154a1d commit 23d6165
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
35 changes: 35 additions & 0 deletions staff/miguel-arias/project/api/handlers/retrieveRoleHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import jwt from 'jsonwebtoken'
const { JsonWebTokenError } = jwt

import { errors } from 'com'
const { NotFoundError, ContentError, TokenError } = errors

import logic from '../logic/index.js'

export default async (req, res) => {
try {
const profileToken = req.headers.authorization.substring(7)
const payload = jwt.verify(profileToken, process.env.JWT_SECRET)
const profileId = payload.sub

const profile = await logic.retrieveRole(profileId)

res.json(profile)

} catch (error) {
let status = 500

if (error instanceof ContentError || error instanceof TypeError)
status = 406

if (error instanceof NotFoundError)
status = 404

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
}

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
24 changes: 24 additions & 0 deletions staff/miguel-arias/project/api/logic/retrieveRole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { validate, errors } from 'com'
const { SystemError, NotFoundError } = errors

import { Profile } from '../data/models.js'

function retrieveRole(profileId) {
validate.id(profileId)

return (async () => {
let profile
try {
profile = await Profile.findById(profileId).lean()
} catch (error) {
throw new SystemError(error.message)
}

if (!profile)
throw new NotFoundError('profile not found')

return profile.role
})()
}

export default retrieveRole
19 changes: 19 additions & 0 deletions staff/miguel-arias/project/api/logic/retrieveRole.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import dotenv from 'dotenv'
dotenv.config()

import mongoose from 'mongoose'

import retrieveRole from './retrieveRole.js'

(async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/test')

const role = await retrieveRole('65d79ed33377222a97582a18')
console.log('role retrieved', role)

await mongoose.disconnect()
} catch (error) {
console.log(error)
}
})()
43 changes: 43 additions & 0 deletions staff/miguel-arias/project/app/src/logic/retrieveRole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { errors } from 'com'
const { SystemError } = errors

import session from './session'

const retrieveRole = () => {
const req = {
method: 'GET',
headers: {
Authorization: `Bearer ${session.profileToken}`
}
}

return (async () => {
let res
try {
res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/role`, req)
} catch (error) {
throw new SystemError(error.message)
}

if (!res.ok) {
let body

try {
body = await res.json()
} catch (error) {
throw new SystemError(error.message)
}

throw new errors[body.error](body.message)
}

try {
const role = await res.json()
return role
} catch (error) {
throw new SystemError(error.message)
}
})()
}

export default retrieveRole

0 comments on commit 23d6165

Please sign in to comment.