Skip to content
Snippets Groups Projects
Commit 4f8fd64a authored by Rafael László's avatar Rafael László :speech_balloon:
Browse files

create user after login

parent 90a8254d
No related branches found
No related tags found
3 merge requests!24Auth, Profile, News, Entry Card, File management,!21update feature/news_api branch,!19Profile and Entry Card
Pipeline #5414 passed
......@@ -14,30 +14,20 @@ const complete = () => async (req: Request, res: Response) => {
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((response) => {
Profile.findOne(
.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) {
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);
}
});
}
if (!!profile) isRegistered = true;
}
}
);
......@@ -45,6 +35,7 @@ const complete = () => async (req: Request, res: Response) => {
id: String(response.data.internal_id),
email: String(response.data.mail),
name: `${response.data.sn} ${response.data.givenName}`,
isRegistered,
token,
};
})
......
import { NextFunction, Request, Response } from "express";
import Profile, { Role } from "../../models/ProfileSchema";
const addUser = () => (req: Request, res: Response, next: NextFunction) => {
const profile = new Profile();
import { ValidationError } from "../utils/ValidationError";
const fields = ["studentCardNumber", "roomNumber", "picture"];
fields.forEach((field) => {
const value = req.body[field];
if (value) profile.set(field, value);
});
const addUser = () => async (
req: Request,
res: Response,
next: NextFunction
) => {
// Already registered
if (req.session?.user?.isRegistered) {
await Profile.findOne(
{ external_id: req.session.user.id },
(error, profile) => {
if (error) {
console.warn(error);
return res.status(400);
} else {
res.status(200);
res.data = { profile };
}
}
);
return next();
}
// Register
try {
const profile = new Profile();
profile.external_id = req.session!.user!.id;
profile.role = Role.User;
const fields = [
{ name: "studentCardNumber", required: true },
{ name: "roomNumber", required: true },
{ name: "picture", required: false },
];
fields.forEach((field) => {
const value = req.body[field.name];
if (field.required && !value) {
res.status(400);
throw new ValidationError(400, `Field: {${field.name}} is required!`);
}
if (value) profile.set(field.name, value);
});
profile.save((err) => {
if (err) {
res.status(400);
} else {
res.status(201);
res.data = { profile };
profile.external_id = req.session!.user!.id;
profile.email = req.session?.user?.email;
profile.name = req.session?.user?.name;
profile.save((err) => {
if (err) {
res.status(400);
} else {
res.status(201);
res.data = { profile };
req.session!.user!.isRegistered = true;
}
next();
});
} catch (error) {
if (error instanceof ValidationError) {
const { code, message } = error;
return res.status(code).send({ message });
}
next();
});
}
};
export default addUser;
export class ValidationError extends Error {
code: number;
constructor(code: number, message: string) {
super(message);
this.code = code;
this.name = "ValidationError";
Object.setPrototypeOf(this, new.target.prototype);
}
}
......@@ -4,5 +4,6 @@ export interface User {
email: string;
name: string;
id: string;
isRegistered: boolean;
token: Token;
}
......@@ -9,6 +9,7 @@ declare global {
newsObject?: INews | null;
profile?: IProfile | null;
profiles?: IProfile[] | null;
error?: string | null;
};
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment