Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/api/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class AuthController {
}

req.session.user = { id: user.id, first: user.first };
await this.userService.updateUser(user.id, { lastLoggedIn: new Date() });
return user;
}

Expand All @@ -40,7 +41,7 @@ export class AuthController {
throw new BadRequest("Email Already Exists");
}

const user = await this.userService.createUser({ first, last, email, password });
const user = await this.userService.createUser({ first, last, email, password, lastLoggedIn: new Date() });
req.session.user = { id: user.id, first: user.first };

sendToWebhook({
Expand All @@ -62,6 +63,7 @@ export class AuthController {
if (!controlled) throw new Unauthorized("Account not found.");

req.session.user = { id: controlled.id, first: user.first, isControlled: true };
await this.userService.updateUser(controlled.id, { lastLoggedIn: new Date() });
return controlled;
}

Expand Down
1 change: 1 addition & 0 deletions packages/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class UserController {

@Get()
async getUser({ session }: Request): Promise<User | null> {
await this.userService.touchLastLoggedIn(session.user.id);
return this.userService.getUserById(session.user.id);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/api/src/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity, Model, Prop } from "@/_lib/mongoose";
import bcrypt from "bcrypt";

@Entity()
@Entity({ timestamps: true })
export class User extends Model {

id: string;
Expand All @@ -23,4 +23,7 @@ export class User extends Model {

@Prop({ type: Boolean, default: false })
synced: boolean;

@Prop({ type: Date, default: null })
lastLoggedIn?: Date;
}
4 changes: 4 additions & 0 deletions packages/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class UserService {
return User.findOne({ email }).lean<User>().exec();
}

async touchLastLoggedIn(id: string): Promise<void> {
await User.findByIdAndUpdate(id, { lastLoggedIn: new Date() }).exec();
}

async updateUser(id: string, data: Partial<User>): Promise<User | null> {
return User.findByIdAndUpdate(id, data).lean<User>().exec();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/app/src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ export default function SettingsPage() {
<div className="flex flex-col gap-1">
<h2 className="text-lg font-semibold text-primary">Profile</h2>
<p className="text-sm text-muted">Manage your account details and privacy.</p>
{user.isSuccess && user.data?.lastLoggedIn && (
<span className="text-xs font-semibold text-primary">
Last logged in: {new Date(user.data.lastLoggedIn).toLocaleString()}
</span>
)}
</div>
{user.isLoading && <span className="text-xs text-muted">Loading...</span>}
</div>
Expand Down