diff --git a/src/middlewares/auth/authenticated.ts b/src/middlewares/auth/authenticated.ts index 8e9d03013726beeb853f6336b5f8dee388edbaf1..503fa2a3a54dd13ee9c419a1ebad43f1b448619a 100644 --- a/src/middlewares/auth/authenticated.ts +++ b/src/middlewares/auth/authenticated.ts @@ -7,7 +7,7 @@ const authenticated = () => ( ) => { if (req.session!.user) next(); else { - res.status(403); + res.status(401); res.json({ message: "You have to login to see this page" }); } }; diff --git a/src/middlewares/auth/complete.ts b/src/middlewares/auth/complete.ts index 28c3f98ffbae2f18719e83a893b9f3e320afe862..e4864b53b442d6c9480686148a9821aec70eae82 100644 --- a/src/middlewares/auth/complete.ts +++ b/src/middlewares/auth/complete.ts @@ -1,6 +1,8 @@ +import Profile, { Role } from "../../models/ProfileSchema"; import { Request, Response } from "express"; import { oauth2, scope } from "../../utils/auth"; +import { authschResponse } from "../../utils/declarations/authschResponse"; import axios from "axios"; const complete = () => async (req: Request, res: Response) => { @@ -13,10 +15,32 @@ const complete = () => async (req: Request, res: Response) => { try { const token = await oauth2().authorizationCode.getToken(tokenConfig); await axios - .get( + .get<authschResponse>( `https://auth.sch.bme.hu/api/profile/?access_token=${token.access_token}` ) .then((response) => { + Profile.findOne( + { external_id: response.data.internal_id }, + (error, profile) => { + if (error) { + console.warn(error); + return res.status(400); + } else { + if (!profile) { + const newProfile = new Profile(); + newProfile.external_id = response.data.internal_id; + newProfile.email = response.data.mail; + newProfile.name = `${response.data.sn} ${response.data.givenName}`; + newProfile.save((err) => { + if (err) { + console.log(err); + return res.status(400); + } + }); + } + } + } + ); req.session!.user = { id: String(response.data.internal_id), email: String(response.data.mail), @@ -27,7 +51,7 @@ const complete = () => async (req: Request, res: Response) => { .catch(function (error) { console.log(error); }); - res.redirect("/"); + return res.redirect("/"); } catch (error) { console.log("Access Token Error", error.message); } diff --git a/src/middlewares/user/getOwnUser.ts b/src/middlewares/user/getOwnUser.ts index d236dd4618a94ca4c234df011cef6a4aa87ee5eb..dd25d51b514b7e3d39916a2a9204b2a72dd020b5 100644 --- a/src/middlewares/user/getOwnUser.ts +++ b/src/middlewares/user/getOwnUser.ts @@ -3,10 +3,17 @@ import { NextFunction, Request, Response } from "express"; import Profile from "../../models/ProfileSchema"; const getOwnUser = () => (req: Request, res: Response, next: NextFunction) => { - if (!req.session!.user) { - return res.json({ loggedIn: false }); - } - res.json({ ...req.session!.user, loggedIn: true }); + console.log(req.session!.user); + Profile.findOne({ external_id: req.session!.user!.id }, (error, profile) => { + if (error) { + console.warn(error); + res.status(400); + } else { + res.status(200); + res.data = { profile }; + } + next(); + }); }; export default getOwnUser; diff --git a/src/models/ProfileSchema.ts b/src/models/ProfileSchema.ts index efb8c59f0d17db1b6d3c8453bd857d82b00bedc1..0d5ffd641c756a5c3a7ef2291559cbf80b9c908a 100644 --- a/src/models/ProfileSchema.ts +++ b/src/models/ProfileSchema.ts @@ -22,7 +22,7 @@ export interface IProfile extends Document { const ProfileSchema = new Schema({ external_id: { type: String, required: true, unique: true, dropDups: true }, - studentCardNumber: { type: String, required: true }, + studentCardNumber: { type: String, required: false }, roomNumber: { type: String }, picture: { type: String }, role: { diff --git a/src/routes/user.ts b/src/routes/user.ts index 4f7c00c65b202645b107cba18074ed342b46fe58..eb3dbfa73868a5c3f671b0011b5c14e5e5f8208b 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -20,7 +20,7 @@ const usersRoute = (app: Application): void => { app.post("/api/v1/users", authenticated(), addUser(), responseUser()); - app.get("/api/v1/users/me", getOwnUser(), responseUser()); + app.get("/api/v1/users/me", authenticated(), getOwnUser(), responseUser()); app.get("/api/v1/users/:id", getUser(), responseUser()); diff --git a/src/utils/declarations/authschResponse.d.ts b/src/utils/declarations/authschResponse.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..6bf887ee2697522f4345dfeb5f4afbf21289c071 --- /dev/null +++ b/src/utils/declarations/authschResponse.d.ts @@ -0,0 +1,20 @@ +export interface authschResponse { + internal_id: string; + displayName: string; + /** surname */ + sn: string; + givenName: string; + mail: string; + linkedAccounts: { + bme: string; + schacc: string; + vir: string; + }; + eduPersonEntitlement: [ + { + id: number; + name: string; + status: string; + } + ]; +}