Browse Source

chore(api): save last login at

pull/19/head
isra el 2 years ago
parent
commit
d2abc28a1a
  1. 11
      api/src/auth/auth.service.ts
  2. 3
      api/src/users/schemas/user.schema.ts

11
api/src/auth/auth.service.ts

@ -43,6 +43,9 @@ export class AuthService {
) )
} }
user.lastLoginAt = new Date()
await user.save()
const payload = { email: user.email, sub: user._id } const payload = { email: user.email, sub: user._id }
return { return {
accessToken: this.jwtService.sign(payload), accessToken: this.jwtService.sign(payload),
@ -64,6 +67,7 @@ export class AuthService {
email, email,
googleId, googleId,
avatar: picture, avatar: picture,
lastLoginAt: new Date(),
}) })
} else { } else {
user.googleId = googleId user.googleId = googleId
@ -74,6 +78,7 @@ export class AuthService {
if (!user.avatar) { if (!user.avatar) {
user.avatar = picture user.avatar = picture
} }
user.lastLoginAt = new Date()
await user.save() await user.save()
} }
@ -89,6 +94,7 @@ export class AuthService {
const user = await this.usersService.create({ const user = await this.usersService.create({
...userData, ...userData,
password: hashedPassword, password: hashedPassword,
lastLoginAt: new Date(),
}) })
const payload = { email: user.email, sub: user._id } const payload = { email: user.email, sub: user._id }
@ -223,7 +229,10 @@ export class AuthService {
user, user,
method, method,
url: url.split('?')[0], url: url.split('?')[0],
ip,
ip:
request.headers['x-forwarded-for'] ||
request.connection.remoteAddress ||
ip,
userAgent, userAgent,
}) })
.catch((e) => { .catch((e) => {

3
api/src/users/schemas/user.schema.ts

@ -28,6 +28,9 @@ export class User {
@Prop({ type: String, default: UserRole.REGULAR }) @Prop({ type: String, default: UserRole.REGULAR })
role: string role: string
@Prop({ type: Date })
lastLoginAt: Date
} }
export const UserSchema = SchemaFactory.createForClass(User) export const UserSchema = SchemaFactory.createForClass(User)
Loading…
Cancel
Save