-
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(authentification): integrated auth0 into frontend and backend, b…
…ut backend pard needs to be tested and fixed in the future
- Loading branch information
Dedakup
committed
Nov 22, 2024
1 parent
5caaf5d
commit f64abc7
Showing
30 changed files
with
4,937 additions
and
1,067 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
.serverless | ||
.env | ||
.env.local |
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,56 @@ | ||
const jwt = require("jsonwebtoken"); | ||
const jwksClient = require("jwks-rsa"); | ||
|
||
const client = jwksClient({ | ||
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`, | ||
}); | ||
|
||
function getKey(header, callback) { | ||
client.getSigningKey(header.kid, (err, key) => { | ||
const signingKey = key.getPublicKey(); | ||
callback(null, signingKey); | ||
}); | ||
} | ||
|
||
module.exports.auth0Authorizer = async (event) => { | ||
const token = event.headers.Authorization || event.headers.authorization; | ||
|
||
if (!token) { | ||
return { | ||
statusCode: 401, | ||
body: JSON.stringify({ message: "Unauthorized" }), | ||
}; | ||
} | ||
|
||
const bearerToken = token.split(" ")[1]; | ||
|
||
try { | ||
const decoded = await new Promise((resolve, reject) => { | ||
jwt.verify( | ||
bearerToken, | ||
getKey, | ||
{ audience: process.env.AUTH0_AUDIENCE, issuer: process.env.AUTH0_ISSUER }, | ||
(err, decoded) => (err ? reject(err) : resolve(decoded)) | ||
); | ||
}); | ||
|
||
return { | ||
principalId: decoded.sub, | ||
policyDocument: { | ||
Version: "2012-10-17", | ||
Statement: [ | ||
{ | ||
Action: "execute-api:Invoke", | ||
Effect: "Allow", | ||
Resource: event.methodArn, | ||
}, | ||
], | ||
}, | ||
}; | ||
} catch (err) { | ||
return { | ||
statusCode: 401, | ||
body: JSON.stringify({ message: "Unauthorized" }), | ||
}; | ||
} | ||
}; |
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 @@ | ||
const db = require('../../utils/db'); | ||
|
||
module.exports.updateBackground = async (event) => { | ||
const userId = event.requestContext.authorizer.userId; | ||
const { background } = JSON.parse(event.body); | ||
|
||
const params = { | ||
TableName: process.env.DYNAMODB_USERS_TABLE, | ||
Key: { userId }, | ||
UpdateExpression: 'set background = :background', | ||
ExpressionAttributeValues: { ':background': background }, | ||
}; | ||
|
||
await db.update(params); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: 'Background updated successfully' }), | ||
}; | ||
}; |
Empty file.
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,11 @@ | ||
module.exports.getMusic = async () => { | ||
const musicList = [ | ||
{ id: 1, title: 'Lo-Fi Beats', url: 'https://example.com/music1.mp3' }, | ||
{ id: 2, title: 'Chill Vibes', url: 'https://example.com/music2.mp3' }, | ||
]; | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(musicList), | ||
}; | ||
}; |
Oops, something went wrong.