Skip to content

Commit d3ebd0b

Browse files
create sign in function (#30)
1 parent 80a7b35 commit d3ebd0b

File tree

4 files changed

+29
-281
lines changed

4 files changed

+29
-281
lines changed

src/.env

Whitespace-only changes.

src/repositories/customer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class CustomerRepository extends AbstractResourceRepository {
1212
getTypeId(): ReferenceTypeId {
1313
return 'customer'
1414
}
15+
1516
create(projectKey: string, draft: CustomerDraft): Customer {
1617
const resource: Customer = {
1718
...getBaseResourceProperties(),
@@ -23,6 +24,7 @@ export class CustomerRepository extends AbstractResourceRepository {
2324
this.save(projectKey, resource)
2425
return resource
2526
}
27+
2628
getMe(projectKey: string): Customer | undefined {
2729
const results = this._storage.query(projectKey, this.getTypeId(), {}) // grab the first customer you can find
2830
if (results.count > 0) {

src/services/my-customer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class MyCustomerService extends AbstractService {
2626

2727
router.post('/signup', this.signUp.bind(this))
2828

29+
router.post('/login', this.signIn.bind(this))
30+
2931
parent.use(`/${basePath}`, router)
3032
}
3133

@@ -43,4 +45,27 @@ export class MyCustomerService extends AbstractService {
4345
const result = this._expandWithId(request, resource.id)
4446
return response.status(this.createStatusCode).send({ customer: result })
4547
}
48+
49+
signIn(request: Request, response: Response) {
50+
const { email, password } = request.body
51+
const encodedPassword = Buffer.from(password).toString('base64')
52+
53+
const result = this.repository.query(request.params.projectKey, {
54+
where: [`email = "${email}"`, `password = "${encodedPassword}"`],
55+
})
56+
57+
if (result.count === 0) {
58+
return response.status(400).send({
59+
message: 'Account with the given credentials not found.',
60+
errors: [
61+
{
62+
code: 'InvalidCredentials',
63+
message: 'Account with the given credentials not found.',
64+
},
65+
],
66+
})
67+
}
68+
69+
return response.status(200).send({ customer: result.results[0] })
70+
}
4671
}

0 commit comments

Comments
 (0)