Skip to content

Commit

Permalink
feat: create user if not exists on login
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhermeasper committed Jul 18, 2024
1 parent 7a1df0f commit 93dd385
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { DiscordService } from '../services/discord';
import { ApiResponse } from '../types';
import { LastfmService } from '../services/lastfm';
import { decryptToken, encryptToken } from '../utils/crypto';
import { UserService } from '../services/user';

class AuthController {
private discordService: DiscordService;
private lastfmService: LastfmService;
private userService: UserService;

constructor() {
this.userService = new UserService();
this.discordService = new DiscordService();
this.lastfmService = new LastfmService();
}
Expand Down Expand Up @@ -49,6 +52,13 @@ class AuthController {
'Authorization, Refresh-Token, Expires-In, Created-At',
);

const discordUser = await this.discordService.getDiscordUser(
response.access_token,
);

if (!(await this.userService.exists(discordUser.id)))
await this.userService.create(discordUser.id);

return res.status(200).json({ message: 'Authenticated successfully' });
} catch (error) {
console.error(error);
Expand Down

0 comments on commit 93dd385

Please sign in to comment.