-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
auth.controller.ts
50 lines (42 loc) 路 1.22 KB
/
auth.controller.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 3p
import {
Context,
dependency,
Get,
HttpResponseRedirect,
setSessionCookie,
} from '@foal/core';
import { FacebookProvider, GoogleProvider } from '@foal/social';
import { TypeORMStore } from '@foal/typeorm';
export class AuthController {
@dependency
google: GoogleProvider;
@dependency
facebook: FacebookProvider;
@dependency
store: TypeORMStore;
@Get('/signin/google')
redirectToGoogle() {
return this.google.redirect();
}
@Get('/signin/google/cb')
async handleGoogleRedirection(ctx: Context) {
const { userInfo } = await this.google.getUserInfo(ctx);
const session = await this.store.createAndSaveSession({ userInfo });
const response = new HttpResponseRedirect('/');
setSessionCookie(response, session.getToken());
return response;
}
@Get('/signin/facebook')
redirectToFacebook() {
return this.facebook.redirect();
}
@Get('/signin/facebook/cb')
async handleFacebookRedirection(ctx: Context) {
const { userInfo } = await this.facebook.getUserInfo(ctx);
const session = await this.store.createAndSaveSession({ userInfo });
const response = new HttpResponseRedirect('/');
setSessionCookie(response, session.getToken());
return response;
}
}