-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): write the mutation to login the user
- Loading branch information
1 parent
e3d8f27
commit dde8c56
Showing
13 changed files
with
60 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { logger } from '../../../configs/logger'; | ||
import { MutationResolvers } from '../../../types/graphql'; | ||
import { createUserSession } from '../../../utils/auth/session'; | ||
import { LOGIN_FAILED_MESSAGE } from '../../../utils/constants'; | ||
import AppError from '../../../utils/errors/app-error'; | ||
|
||
export const loginUser: MutationResolvers['loginUser'] = async (_parent, args, context) => { | ||
try { | ||
const { email, password } = args; | ||
const user = await context.db.user.login(email, password); | ||
|
||
const session = await createUserSession(user.id); | ||
|
||
return { token: session.token }; | ||
} catch (err) { | ||
logger.error(err); | ||
|
||
throw new AppError(LOGIN_FAILED_MESSAGE, 'LOGIN_FAILED'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
type LoginResult { | ||
token: String! | ||
} | ||
|
||
type Query { | ||
authenticatedUser: User | ||
} | ||
|
||
type Mutation { | ||
loginUser(email: String!, password: String!): LoginResult! | ||
logoutUser: Boolean! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,12 @@ export default class UserService { | |
return this._userRepository.update(currentUser.id, updateUserDto.toUser(currentUser)); | ||
} | ||
|
||
async loadAdminUsers(role: Role): Promise<void> { | ||
async loadAdminUser(role: Role, adminPassword: string): Promise<void> { | ||
const userAdminDto = new CreateUserDto({ | ||
email: '[email protected]', | ||
name: 'Eric Teco', | ||
oauthProvider: 'github', | ||
oauthProvider: 'email', | ||
password: adminPassword, | ||
pictureUrl: null, | ||
roleId: role.id, | ||
timezone: 'Europe/Paris', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,9 @@ describe('Test User service', () => { | |
|
||
it('should load users in the database', async () => { | ||
const [roleAdmin] = await roleService.findAll(); | ||
const adminPassword = 'VerStrongPassword'; | ||
|
||
await userService.loadAdminUsers(roleAdmin); | ||
await userService.loadAdminUser(roleAdmin, adminPassword); | ||
|
||
const adminUser = await userService.findByEmail('[email protected]'); | ||
|
||
|