import { UserInfo } from './../../../../models/userInfo' ; import { AuthProvider } from './../../../../models/user' ; import { UserRepository } from '../../repositories/user.repository' ; import { AccountRepository } from '../../repositories/account.repository' ; import { CategoriesRepository } from '../../../settings/categories/categories.repository' ; import { InMemoryUserRepository } from '../../repositories/in-memory/in-memory-user.repository' ; import { InMemoryAccountRepository } from '../../repositories/in-memory/in-memory-account.repository' ; import { InMemoryCategoriesRepository } from '../../../settings/categories/in-memory-categories.repository' ; import { getExternalAuthProvider } from './external-auth.factory' ; import log from '../../../../utils/logger' ; const userRepository = new InMemoryUserRepository ( ) ; const accountRepository = new InMemoryAccountRepository ( ) ; const categoriesRepository = new InMemoryCategoriesRepository ( ) ; export class ExternalAuthService { login ( provider , authCode , session ) { const authProvider = getExternalAuthProvider ( provider ) ; return authProvider . getAccessToken ( authCode , 'login' ) . then ( ( token ) => authProvider . getUserInfo ( token ) . then ( userInfo => userRepository . getUserByExternalId ( provider , userInfo . id ) . then ( user => { session . user = user ; log . info ( `auth.${ provider }.session_login_successful` , { user } ) ; } ) . catch ( ( ) => { log . error ( `auth.${ provider }.session_login_failed` , { userInfo } ) ; return Promise . reject ( 'User not found' ) ; } ) ) ) ; } signup ( provider , authCode , session ) { const authProvider = getExternalAuthProvider ( provider ) ; return authProvider . getAccessToken ( authCode , 'signup' ) . then ( ( token ) => authProvider . getUserInfo ( token ) . then ( ( userInfo ) => userRepository . assertUserWithExternalIdNotExist ( provider , userInfo . id ) . then ( ( ) => this . doSignup ( provider , userInfo ) . then ( ( ) => { log . info ( `auth.${ provider }.signup_successful` , { email : userInfo . email } ) ; userRepository . getUserByExternalId ( provider , userInfo . id ) . then ( user => { session . user = user ; log . info ( `auth.${ provider }.session_login_successful` , { user } ) ; } ) ; } ) . catch ( error => { log . error ( `auth.${ provider }.signup_failed` , { email : userInfo . email } ) ; throw error ; } ) ) ) ) ; } private doSignup ( provider , userInfo ) { return accountRepository . createAccount ( { } ) . then ( accountId => Promise . all ( [ categoriesRepository . createDefaultCategories ( accountId ) , userRepository . createUser ( { accountId : accountId , email : userInfo . email , role : 'OWNER' , confirmed : true , createdWith : provider as AuthProvider , externalId : { [ provider ] : userInfo . id } } ) ] ) ) ; } }