Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
auth-service
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Group 5
auth-service
Commits
b4d5f557
Commit
b4d5f557
authored
2 years ago
by
Everett, Josh (UG - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
registration start
parent
f06dec45
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+30
-0
30 additions, 0 deletions
README.md
src/login.ts
+7
-6
7 additions, 6 deletions
src/login.ts
src/register.ts
+63
-0
63 additions, 0 deletions
src/register.ts
with
100 additions
and
6 deletions
README.md
0 → 100644
+
30
−
0
View file @
b4d5f557
# Auth Service
# Endpoints
## Login
```
/auth/login
```
Request format:
```
json
{
"email"
:
"joebloggs@gmail.com"
,
"password"
:
"password123"
}
```
Responses:
## Register
```
/auth/register
```
# JWT Validation
How to validate the JWT and extract the user informtion from it
This diff is collapsed.
Click to expand it.
src/login.ts
+
7
−
6
View file @
b4d5f557
import
{
Request
,
Response
}
from
"
express
"
;
import
{
Request
,
Response
}
from
"
express
"
;
import
Joi
from
"
joi
"
;
import
Joi
from
"
joi
"
;
import
{
TokenService
}
from
"
./token_service
"
;
import
{
ITokenPayload
,
TokenService
}
from
"
./token_service
"
;
interface
ILoginResponse
{
token
:
string
;
}
interface
ILoginRequest
{
interface
ILoginRequest
{
email
:
string
;
email
:
string
;
...
@@ -16,7 +14,7 @@ export class LoginHandler {
...
@@ -16,7 +14,7 @@ export class LoginHandler {
}
}
public
async
handle
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
>
{
public
async
handle
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
>
{
console
.
log
(
req
.
body
);
const
schema
=
Joi
.
object
({
const
schema
=
Joi
.
object
({
email
:
Joi
.
string
().
email
().
required
(),
email
:
Joi
.
string
().
email
().
required
(),
password
:
Joi
.
string
().
min
(
8
).
required
()
password
:
Joi
.
string
().
min
(
8
).
required
()
...
@@ -46,8 +44,11 @@ export class LoginHandler {
...
@@ -46,8 +44,11 @@ export class LoginHandler {
}
}
}
}
p
rivate
async
login
(
req
:
ILoginRequest
):
Promise
<
I
LoginResponse
|
null
>
{
p
ublic
async
login
(
req
:
ILoginRequest
):
Promise
<
I
TokenPayload
|
null
>
{
// fetch from the database the following user object and if the user exists and the password hash matches the password hash in the database
// then return the token
// otherwise return null
const
user
:
IUser
=
{
const
user
:
IUser
=
{
id
:
`id-
${
req
.
email
}
`
,
id
:
`id-
${
req
.
email
}
`
,
email
:
req
.
email
,
email
:
req
.
email
,
...
...
This diff is collapsed.
Click to expand it.
src/register.ts
0 → 100644
+
63
−
0
View file @
b4d5f557
import
Joi
from
"
joi
"
;
import
{
Request
,
Response
}
from
"
express
"
;
import
{
ITokenPayload
,
TokenService
}
from
"
./token_service
"
;
interface
IRegistrationRequest
{
email
:
string
;
password
:
string
;
fullName
:
string
;
}
export
class
RegistrationHandler
{
constructor
(
private
tokenService
:
TokenService
){}
public
async
handle
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
>
{
const
schema
=
Joi
.
object
({
email
:
Joi
.
string
().
email
().
required
(),
password
:
Joi
.
string
().
min
(
8
).
required
(),
fullName
:
Joi
.
string
().
required
()
})
try
{
const
{
value
,
error
}
=
schema
.
validate
(
req
.
body
);
if
(
error
)
{
console
.
log
(
'
validation error:
'
,
error
.
message
);
return
res
.
status
(
400
).
send
({
message
:
error
!
.
message
});
}
const
{
email
,
password
,
fullName
}
=
value
;
const
responseBody
=
await
this
.
register
({
email
,
password
,
fullName
});
if
(
!
responseBody
)
{
return
res
.
status
(
403
).
send
({
message
:
'
There already exists a user with that email address
'
});
}
return
res
.
status
(
200
).
send
(
responseBody
);
}
catch
(
err
)
{
console
.
log
(
'
Error:
'
,
err
);
return
res
.
status
(
500
).
send
(
err
);
}
}
public
async
register
(
req
:
IRegistrationRequest
):
Promise
<
ITokenPayload
|
null
>
{
// Check that there isn't already a user in the database with the same email address
// If there is then return null
// Otherwise create a new user in the database and return the token
const
user
:
IUser
=
{
id
:
`id-
${
req
.
email
}
`
,
email
:
req
.
email
,
password
:
req
.
password
,
fullName
:
req
.
fullName
,
}
const
token
=
await
this
.
tokenService
.
generateToken
(
user
);
return
token
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment