Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (await bcrypt.compare(credentials.password, user.hashedPassword)) {
if (!user.emailConfirmed) {
throw new BadRequestError("You must have a confirmed email to log in.");
}
let token = jwt.sign({ userId: "1" }, config.get("jwt.key"), {
expiresIn: config.get("jwt.expiry")
});
return { token };
} else {
throw new BadRequestError(invalidCredentialsMessage);
}
}
@Post("/register")
@OnUndefined(204)
async register(@Body() credentials: AuthCredentialsNew) {
// Create a new user
let newUser = new User();
newUser.email = credentials.email;
let hashedPassword = await bcrypt.hash(credentials.password, 3);
newUser.hashedPassword = hashedPassword;
await this.getRepo().persist(newUser);
// Send confirmation email
let confirmEmailToken = jwt.sign(
{ userId: newUser.id },
config.get("jwt.key"),
{ expiresIn: config.get("jwt.expiry") }
);
let emailConfirmUrl = `${config.get(
"frontend_url"
/**
* @api {post} /api/auth/register Register user
* @apiName PostAuthRegister
* @apiGroup Auth
*
* @apiParam {IUser} user New user to be registered.
*
* @apiError BadRequestError That matriculation number is already in use
* @apiError BadRequestError That email address is already in use
* @apiError BadRequestError You can only sign up as student or teacher
* @apiError BadRequestError You are not allowed to register as teacher
* @apiError InternalServerError Could not send E-Mail
*/
@Post('/register')
@OnUndefined(204)
async postRegister(@Body() user: IUser) {
const existingUser = await User.findOne({$or: [{email: user.email}, {uid: user.uid}]});
// If user is not unique, return error
if (existingUser) {
if (user.role === 'student' && existingUser.uid === user.uid) {
throw new BadRequestError(errorCodes.errorCodes.duplicateUid.code);
}
if (existingUser.email === user.email) {
throw new BadRequestError(errorCodes.errorCodes.mail.duplicate.code);
}
}
if (user.role !== 'teacher' && user.role !== 'student') {
throw new BadRequestError('You can only sign up as student or teacher');
}
if (user.role === 'teacher' && (typeof user.email !== 'string' || !user.email.match(config.teacherMailRegex))) {
throw new BadRequestError(errorCodes.errorCodes.mail.noTeacher.code);
const openid = res.data.openid
const oldUser = await this.userService.userModel.findOne({openid: openid})
if (oldUser) {
return this.userService.signUser(oldUser)
} else {
const newUser = await this.userService.createNewCustomer(openid)
return this.userService.signUser(newUser)
}
} catch (e) {
console.log(e)
throw new UnauthorizedError()
}
}
@Post('/userinfo')
@OnUndefined(204)
async saveUserInfoFromWechat(@BodyParam('avatarUrl', {required: true}) avatarUrl: string,
@BodyParam('nickName', {required: true}) nickName: string,
@State('user') user: Payload): Promise {
await this.userService.userModel.update({_id: user.id}, {
avatarUrl: avatarUrl,
nickName: nickName
})
}
@Post('/share')
@OnUndefined(204)
shareEvaluate(@State('user') user: Payload, @BodyParam('shareKey', {required: true}) shareKey: string): void {
this.userService.shareModel.create({
user: user.id,
key: shareKey,
expiresAt: Date.now() + ms(Environment.shareKeyExpires)
* @apiName ActivationResend
* @apiGroup Auth
*
* @apiParam {string} lastname lastname of user which activation should be resend.
* @apiParam {string} uid matriculation number of user which activation should be resend.
* @apiParam {string} email email the new activation should be sent to.
*
* @apiError (BadRequestError) 400 User was not found.
* @apiError (BadRequestError) 400 That email address is already in use
* @apiError (BadRequestError) 400 User is already activated.
* @apiError (HttpError) 503 You can only resend the activation every X minutes. Your next chance is in
* time left till next try in 'try-after' header in seconds
* @apiError (InternalServerError) Could not send E-Mail
*/
@Post('/activationresend')
@OnUndefined(204)
async activationResend (@BodyParam('lastname') lastname: string,
@BodyParam('uid') uid: string,
@BodyParam('email') email: string,
@Res() response: Response) {
const user = await User.findOne({'profile.lastName': lastname, uid: uid, role: 'student'});
if (!user) {
throw new BadRequestError(errorCodes.errorCodes.user.userNotFound.code);
}
if (user.isActive) {
throw new BadRequestError(errorCodes.errorCodes.user.userAlreadyActive.code);
}
const timeSinceUpdate: number = (Date.now() - user.updatedAt.getTime() ) / 60000;
if (timeSinceUpdate < Number(config.timeTilNextActivationResendMin)) {
async post(@Body() contact: Contact) {
return await this.getRepo().persist(contact);
}
@Put("/:id")
@OnUndefined(404)
async put(@Param("id") id: number, @Body() contact: Contact) {
let existingContact = await this.getRepo().findOneById(id);
if (existingContact) {
Object.assign(existingContact, contact);
return await this.getRepo().persist(existingContact);
}
}
@Delete("/:id")
@OnUndefined(204)
async remove(@Param("id") id: number) {
let existingContact = await this.getRepo().findOneById(id);
if (!existingContact) {
throw new NotFoundError("Not found");
} else {
let result = await this.getRepo().remove(existingContact);
}
}
private getRepo() {
return getConnection().getRepository(Contact);
}
}
constructor(
private userService: UserService
) { }
@Get()
public find(): Promise {
return this.userService.find();
}
@Get('/me')
public findMe(@Req() req: any): Promise {
return req.user;
}
@Get('/:id')
@OnUndefined(UserNotFoundError)
public one(@Param('id') id: string): Promise {
return this.userService.findOne(id);
}
@Post()
public create(@Body() user: User): Promise {
return this.userService.create(user);
}
@Put('/:id')
public update(@Param('id') id: string, @Body() user: User): Promise {
return this.userService.update(id, user);
}
@Delete('/:id')
public delete(@Param('id') id: string): Promise {
}
@Get("/:id")
async getOne(@Param("id") id: number) {
if (id) {
return await this.getRepo().findOneById(id);
}
}
@Post("/")
async post(@Body() contact: Contact) {
return await this.getRepo().persist(contact);
}
@Put("/:id")
@OnUndefined(404)
async put(@Param("id") id: number, @Body() contact: Contact) {
let existingContact = await this.getRepo().findOneById(id);
if (existingContact) {
Object.assign(existingContact, contact);
return await this.getRepo().persist(existingContact);
}
}
@Delete("/:id")
@OnUndefined(204)
async remove(@Param("id") id: number) {
let existingContact = await this.getRepo().findOneById(id);
if (!existingContact) {
throw new NotFoundError("Not found");
} else {
let result = await this.getRepo().remove(existingContact);
}
}
@Post('/userinfo')
@OnUndefined(204)
async saveUserInfoFromWechat(@BodyParam('avatarUrl', {required: true}) avatarUrl: string,
@BodyParam('nickName', {required: true}) nickName: string,
@State('user') user: Payload): Promise {
await this.userService.userModel.update({_id: user.id}, {
avatarUrl: avatarUrl,
nickName: nickName
})
}
@Post('/share')
@OnUndefined(204)
shareEvaluate(@State('user') user: Payload, @BodyParam('shareKey', {required: true}) shareKey: string): void {
this.userService.shareModel.create({
user: user.id,
key: shareKey,
expiresAt: Date.now() + ms(Environment.shareKeyExpires)
})
}
@Get('/share/:shareKey')
async getShareEvaluateData(@Param('shareKey') shareKey: string) {
const s = await this.userService.shareModel.findOne({key: shareKey})
.populate('user')
if (new Date() > s.expiresAt) {
throw new BadRequestError()
} else {
@Authorized()
@JsonController('/pets')
export class PetController {
constructor(
private petService: PetService
) { }
@Get()
public find(): Promise {
return this.petService.find();
}
@Get('/:id')
@OnUndefined(PetNotFoundError)
public one(@Param('id') id: string): Promise {
return this.petService.findOne(id);
}
@Post()
public create(@Body() pet: Pet): Promise {
return this.petService.create(pet);
}
@Put('/:id')
public update(@Param('id') id: string, @Body() pet: Pet): Promise {
return this.petService.update(id, pet);
}
@Delete('/:id')
public delete(@Param('id') id: string): Promise {