-
Rafael László authoredRafael László authored
complete.ts 1.52 KiB
import Profile, { Role } from "../../models/ProfileSchema";
import { Request, Response } from "express";
import { oauth2, scope } from "../../utils/auth";
import { authschResponse } from "../../utils/types/authschResponse";
import axios from "axios";
const complete = () => async (req: Request, res: Response) => {
const tokenConfig = {
code: req.query.code,
scope: scope,
redirect_uri: "",
};
try {
const token = await oauth2().authorizationCode.getToken(tokenConfig);
let isRegistered = false;
await axios
.get<authschResponse>(
`https://auth.sch.bme.hu/api/profile/?access_token=${token.access_token}`
)
.then(async (response) => {
await Profile.findOne(
{ external_id: response.data.internal_id },
(error, profile) => {
if (error) {
console.warn(error);
return res.status(400);
} else {
if (!!profile) isRegistered = true;
}
}
);
req.session!.user = {
id: String(response.data.internal_id),
email: String(response.data.mail),
name: `${response.data.sn} ${response.data.givenName}`,
isRegistered,
token,
};
})
.catch(function (error) {
console.log(error);
});
return res.redirect(process.env.REDIRECT_URI || "/");
} catch (error) {
console.log("Access Token Error", error.message);
}
};
export default complete;