1
- import { User , DatabaseInterface , AuthenticationService } from '@accounts/types' ;
2
- import { AccountsServer , ServerHooks } from '@accounts/server' ;
3
- import { OAuthOptions } from './types/oauth-options' ;
1
+ import {
2
+ User ,
3
+ DatabaseInterface ,
4
+ AuthenticationService ,
5
+ DatabaseInterfaceSessions ,
6
+ DatabaseInterfaceUser ,
7
+ } from '@accounts/types' ;
8
+ import {
9
+ AccountsServer ,
10
+ /*DatabaseInterfaceSessionsToken,*/ DatabaseInterfaceUserToken ,
11
+ ServerHooks ,
12
+ } from '@accounts/server' ;
13
+ import { OAuthProviders } from './types/oauth-providers' ;
4
14
import { OAuthUser } from './types/oauth-user' ;
15
+ import { ExecutionContext , Inject , Injectable } from 'graphql-modules' ;
16
+ import { OAuthProvidersToken } from './types/OAuthProviders.symbol' ;
5
17
6
18
const getRegistrationPayloadDefault = async ( oauthUser : OAuthUser ) => {
7
19
return {
8
20
email : oauthUser . email ,
9
21
} ;
10
22
} ;
11
23
12
- export class AccountsOauth implements AuthenticationService {
24
+ @Injectable ( {
25
+ global : true ,
26
+ } )
27
+ export class AccountsOauth < CustomUser extends User = User >
28
+ implements AuthenticationService < CustomUser >
29
+ {
30
+ @ExecutionContext ( ) public context ! : ExecutionContext ;
13
31
public server ! : AccountsServer ;
14
32
public serviceName = 'oauth' ;
15
- private db ! : DatabaseInterface ;
16
- private options : OAuthOptions ;
33
+ private db ! : DatabaseInterfaceUser < CustomUser > ;
34
+ // private dbSessions!: DatabaseInterfaceSessions;
35
+
36
+ constructor (
37
+ @Inject ( OAuthProvidersToken ) public oauthProviders : OAuthProviders ,
38
+ @Inject ( DatabaseInterfaceUserToken )
39
+ db ?: DatabaseInterface < CustomUser > | DatabaseInterfaceUser < CustomUser > ,
40
+ // @Inject (DatabaseInterfaceSessionsToken) dbSessions?: DatabaseInterfaceSessions,
41
+ server ?: AccountsServer
42
+ ) {
43
+ if ( db ) {
44
+ this . db = db ;
45
+ // this.dbSessions = dbSessions ?? (db as DatabaseInterfaceSessions);
46
+ }
47
+
48
+ if ( server ) {
49
+ this . server = server ;
50
+ }
51
+ }
17
52
18
- constructor ( options : OAuthOptions ) {
19
- this . options = options ;
53
+ private getOAuthProvider ( providerName : string ) {
54
+ const instanceOrCtor = this . oauthProviders [ providerName ] ;
55
+ // If it's a constructor we use dependency injection (GraphQL), otherwise we already have an instance (REST)
56
+ const provider =
57
+ typeof instanceOrCtor === 'function'
58
+ ? this . context . injector . get ( instanceOrCtor )
59
+ : instanceOrCtor ;
60
+ if ( ! provider ) {
61
+ throw new Error ( `No OAuth provider with the name ${ providerName } was registered.` ) ;
62
+ }
63
+ return provider ;
20
64
}
21
65
22
- public setStore ( store : DatabaseInterface ) {
66
+ public getOAuthProviders ( ) : OAuthProviders {
67
+ return this . oauthProviders ;
68
+ }
69
+
70
+ public setUserStore ( store : DatabaseInterfaceUser < CustomUser > ) {
23
71
this . db = store ;
24
72
}
25
73
26
- public async authenticate ( params : any ) : Promise < User | null > {
27
- if ( ! params . provider || ! this . options [ params . provider ] ) {
74
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
75
+ public setSessionsStore ( store : DatabaseInterfaceSessions ) {
76
+ //this.dbSessions = store;
77
+ }
78
+
79
+ public async authenticate ( params : any ) : Promise < CustomUser | null > {
80
+ if ( ! params . provider || ! this . getOAuthProvider ( params . provider ) ) {
28
81
throw new Error ( 'Invalid provider' ) ;
29
82
}
30
83
31
- const userProvider = this . options [ params . provider ] ;
84
+ const userProvider = this . getOAuthProvider ( params . provider ) ;
32
85
33
86
if ( typeof userProvider . authenticate !== 'function' ) {
34
87
throw new Error ( 'Invalid provider' ) ;
@@ -49,7 +102,11 @@ export class AccountsOauth implements AuthenticationService {
49
102
50
103
const userId = await this . db . createUser ( userData ) ;
51
104
52
- user = ( await this . db . findUserById ( userId ) ) as User ;
105
+ user = await this . db . findUserById ( userId ) ;
106
+
107
+ if ( user == null ) {
108
+ throw new Error ( `Cannot find user ${ userId } ` ) ;
109
+ }
53
110
54
111
if ( this . server ) {
55
112
await this . server . getHooks ( ) . emit ( ServerHooks . CreateUserSuccess , user ) ;
@@ -69,7 +126,7 @@ export class AccountsOauth implements AuthenticationService {
69
126
}
70
127
71
128
public async unlink ( userId : string , provider : string ) {
72
- if ( ! provider || ! this . options [ provider ] ) {
129
+ if ( ! provider || ! this . getOAuthProvider ( provider ) ) {
73
130
throw new Error ( 'Invalid provider' ) ;
74
131
}
75
132
0 commit comments