Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----app.ts--------
- // unauthenticated routes
- app.use("/api/v1/login", login);
- // authenticated routes
- app.use(authMiddleware);
- -----login.ts--------
- router.get("/", async (req, res) => {
- logger.info(`Initiating oauth request, redirecting to ${authorizationUri}`);
- res.redirect(authorizationUri);
- });
- router.get("/callback", async (req, res, next) => {
- const token = await fafOAuth.code.getToken(req.originalUrl).catch(next);
- const apiAccessor = new FafApiAccessor(token);
- const player = await apiAccessor.getPlayerMe().catch(next);
- const {id, login} = player.data.data.attributes;
- if ((await getUser(id)) == null) {
- await insertNewUsers([{id, username: login}]).catch(next);
- } else {
- await updateUser(id, login).catch(next);
- }
- res.send({message: "Successfully logged in"});
- });
- -----authMiddlware.ts--------
- export default function authHandler(req: Request, res: Response, next: NextFunction) {
- const token = req.headers.authorization;
- if (token) {
- njwt.verify(token, SIGNING_KEY, (err, ver) => {
- if (err) {
- next(new TokenExpired());
- } else {
- (<IAuthedRequest> req).user = ver.body;
- next();
- }
- });
- } else {
- // token not sent
- next(new UnauthorizedAccess());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement