forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functionality of role retrieval, to use in whole app b00tc4mp#425
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
staff/miguel-arias/project/api/handlers/retrieveRoleHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |