-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#19): view current location as a driver i want to see my current…
… location on a map in real time so that i can track my position and navigate the area effectively - adding JWT auth token support (refresh token, auth token)
- Loading branch information
1 parent
0a1a56d
commit c181a51
Showing
10 changed files
with
114 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Context } from 'koa'; | ||
import Driver from '../models/driver'; | ||
import { generateAccessToken } from '../utils/token-utils'; | ||
|
||
export const refreshAccessToken = async (ctx: Context) => { | ||
const { refreshToken } = ctx.request.body as any; | ||
if (!refreshToken) { | ||
ctx.status = 400; | ||
ctx.body = { error: 'Refresh token is required.' }; | ||
return; | ||
} | ||
|
||
const driver = await Driver.findOne({ where: { refreshToken } }); | ||
if (!driver) { | ||
ctx.status = 403; | ||
ctx.body = { error: 'Invalid refresh token.' }; | ||
return; | ||
} | ||
|
||
const accessToken = generateAccessToken(driver.dataValues.id); | ||
ctx.status = 200; | ||
ctx.body = { accessToken }; | ||
}; |
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,29 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { Context, Next } from 'koa'; | ||
import Driver from '../models/driver'; | ||
|
||
const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET || 'access-secret'; | ||
|
||
export const authenticateToken = async (ctx: Context, next: Next) => { | ||
const token = ctx.headers['authorization']?.split(' ')[1]; | ||
if (!token) { | ||
ctx.status = 401; | ||
ctx.body = { error: 'Access token required.' }; | ||
return; | ||
} | ||
|
||
try { | ||
const decoded = jwt.verify(token, ACCESS_TOKEN_SECRET) as { driverId: number }; | ||
const driver = await Driver.findByPk(decoded.driverId); | ||
if (!driver) { | ||
ctx.status = 403; | ||
ctx.body = { error: 'Invalid access token.' }; | ||
return; | ||
} | ||
ctx.state.driver = driver; | ||
await next(); | ||
} catch (error) { | ||
ctx.status = 403; | ||
ctx.body = { error: 'Invalid or expired access token.' }; | ||
} | ||
}; |
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,15 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET || 'access-secret'; | ||
const REFRESH_TOKEN_SECRET = process.env.REFRESH_TOKEN_SECRET || 'refresh-secret'; | ||
const ACCESS_TOKEN_EXPIRATION = '1d'; | ||
const REFRESH_TOKEN_EXPIRATION = '7d'; | ||
|
||
export const generateAccessToken = (driverId: string) => { | ||
return jwt.sign({ driverId }, ACCESS_TOKEN_SECRET, { expiresIn: ACCESS_TOKEN_EXPIRATION }); | ||
}; | ||
|
||
export const generateRefreshToken = () => { | ||
return uuidv4(); // Generates a unique ID for the refresh token | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export const MQTT_BROKER_URL = '192.168.68.106'; | ||
export const MQTT_PORT = 8883; | ||
export const MQTT_TOPIC = 'morro-taxi/location'; | ||
export const MQTT_TOPIC = '/drivers/${driver_id}/location'; |
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 |
---|---|---|
|
@@ -3518,6 +3518,13 @@ | |
resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz" | ||
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== | ||
|
||
"@types/jsonwebtoken@^9.0.7": | ||
version "9.0.7" | ||
resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-9.0.7.tgz#e49b96c2b29356ed462e9708fc73b833014727d2" | ||
integrity sha512-ugo316mmTYBl2g81zDFnZ7cfxlut3o+/EQdaP7J8QN2kY6lJ22hmQYCK5EHcJHbrW+dkCGSCPgbG8JtYj6qSrg== | ||
dependencies: | ||
"@types/node" "*" | ||
|
||
"@types/keygrip@*": | ||
version "1.0.6" | ||
resolved "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.6.tgz" | ||
|
@@ -12816,6 +12823,11 @@ [email protected]: | |
resolved "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz" | ||
integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== | ||
|
||
uuid@^11.0.2: | ||
version "11.0.2" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-11.0.2.tgz#a8d68ba7347d051e7ea716cc8dcbbab634d66875" | ||
integrity sha512-14FfcOJmqdjbBPdDjFQyk/SdT4NySW4eM0zcG+HqbHP5jzuH56xO3J1DGhgs/cEMCfwYi3HQI1gnTO62iaG+tQ== | ||
|
||
uuid@^7.0.3: | ||
version "7.0.3" | ||
resolved "https://registry.npmjs.org/uuid/-/uuid-7.0.3.tgz" | ||
|