Skip to content

Commit

Permalink
fix(auth): Return role when logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
HagerDakroury committed Jul 12, 2021
1 parent b8cba65 commit 64e28c5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
134 changes: 70 additions & 64 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,110 @@

- **signup**

POST
POST

http://localhost:5000/api/v1/auth/signup
http://localhost:5000/api/v1/auth/signup

- Example Request
- role → "student" | "instructor"
- password 4-16 letters
- Example Request

```json
{
"email":"[email protected]",
"password":"12345678",
"role":"student"
}
```
- role → "student" | "instructor"
- password 4-16 letters

- Response
- account created
```json
{
"email": "[email protected]",
"password": "12345678",
"role": "student"
}
```

201 - token
- Response

```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk"
}
```
- account created

- validation errors
201 - token

400 - "Email not Valid", "Password must be between 4 and 16 characters", "No role provided"
```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk"
}
```

- duplicate accound
- validation errors

400 - "Account already exists"
400 - "Email not Valid", "Password must be between 4 and 16 characters", "No role provided"

- Any other error (e.g db connection)
- duplicate accound

400 - error message
400 - "Account already exists"

- Any other error (e.g db connection)

400 - error message

- **login**

POST
POST

http://localhost:5000/api/v1/auth/login

http://localhost:5000/api/v1/auth/login
- Example Request

- Example Request
```json
{
"email": "[email protected]",
"password": "12345678"
}
```

```json
{
"email":"[email protected]",
"password":"12345678"
}
```
- Response

- Response
- logged in
- logged in

201 - token
201 - token

```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk"
}
```
```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk",
"role": "instructor"
}
```

- validation errors
- validation errors

400 - "Email not found", "Password is not correct"
400 - "Email not found", "Password is not correct"

- Any other error (e.g db connection)
- Any other error (e.g db connection)

400 - error message
400 - error message

- **verify**

POST
POST

http://localhost:5000/api/v1/auth/verify

- Example Request

http://localhost:5000/api/v1/auth/verify
- Header → Authorization = Bearer token
- optional body, for role verification

- Example Request
- Header → Authorization = Bearer token
- optional body, for role verification
```json
{
"role": "instructor"
}
```

```json
{
"role":"instructor"
}
```
- Response

- Response
- verified
- verified

200 - verified
200 - verified

- validation errors
- validation errors

400 - "Invalid token", "No token provided"
400 - "Invalid token", "No token provided"

- unauthorized role
- unauthorized role

400 - "Role not authorized"
400 - "Role not authorized"
2 changes: 1 addition & 1 deletion auth/src/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ router.post(`${baseUrl}/login`, async (req, res) => {

const userJWT = createAccessToken({ email, role });

return res.status(201).send({ token: userJWT });
return res.status(201).send({ token: userJWT, role: role });
} catch (error) {
return res.status(400).send(error.message);
}
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ services:
image: edugaze.development:0.0.1
environment:
NODE_ENV: development
PORT: 5000
PORT: 4002
build:
context: auth/
target: dev
ports:
- 5000:3000
- 4002:4002
volumes:
- ./auth:/usr/src/app/
depends_on:
Expand Down

0 comments on commit 64e28c5

Please sign in to comment.