-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOAuth.ts
More file actions
34 lines (29 loc) · 1.31 KB
/
OAuth.ts
File metadata and controls
34 lines (29 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { githubClient, User as GitHubUser } from 'mobx-github';
import { Body, HttpCode, JsonController, Post } from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import { isDeepStrictEqual } from 'util';
import { OAuthSignInData, User } from '../model';
import { activityLogService, sessionService } from '../service';
@JsonController('/user/OAuth')
export class OauthController {
userStore = sessionService.userStore;
@Post('/GitHub')
@HttpCode(201)
@ResponseSchema(User)
async signInWithGithub(@Body() { accessToken }: OAuthSignInData) {
const { body } = await githubClient.get<GitHubUser>('user', {
Authorization: `Bearer ${accessToken}`
});
const { email, login, avatar_url } = body!;
const user =
(await this.userStore.findOneBy({ email })) ||
(await sessionService.signUp({ email, password: accessToken }));
const newProfile = { name: login, avatar: avatar_url },
oldPofile = { name: user.name, avatar: user.avatar };
if (!isDeepStrictEqual(oldPofile, newProfile)) {
await this.userStore.save(Object.assign(user, newProfile));
await activityLogService.logUpdate(user, 'User', user.id);
}
return sessionService.sign(user);
}
}