-
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.
Merge branch 'refs/heads/backend-lambda' into Bugs-and-errors
# Conflicts: # backend-lambda/OAuthProcessing/index.mjs # backend-lambda/getMaintenance/index.mjs # backend-lambda/getUserVehicleOdometers/index.mjs # frontend/src/pages/garage/AddVehicle.js # frontend/src/pages/vehicle/VehicleInfo.js
- Loading branch information
Showing
23 changed files
with
454 additions
and
359 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"dotenv": "^16.4.5", | ||
"jsonwebtoken": "^9.0.2", | ||
"mongodb": "^6.5.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,56 +1,42 @@ | ||
import { MongoClient, ServerApiVersion } from 'mongodb'; | ||
import 'dotenv/config'; | ||
import jwt from 'jsonwebtoken'; | ||
import {MongoClient, ServerApiVersion} from 'mongodb'; | ||
import 'dotenv/config'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
export const handler = async (event, context) => { | ||
const uri = process.env.MONGO_URI; | ||
const client = new MongoClient(uri, { | ||
export const handler = async (event, context) => { | ||
const uri = process.env.MONGO_URI; | ||
const client = new MongoClient(uri, { | ||
serverApi: { | ||
version: ServerApiVersion.v1, | ||
strict: true, | ||
deprecationErrors: true, | ||
} | ||
} | ||
); | ||
const authorization = event.headers['authorization']; | ||
console.log(authorization); | ||
}); | ||
const authorization = event.headers['Authorization']; | ||
console.log(event); | ||
|
||
try { | ||
await client.connect(); | ||
|
||
if (authorization) { | ||
const token = authorization.split(' ')[1]; | ||
console.log(`token --- ${token}`); | ||
try { | ||
return client.connect() | ||
.then(async () => { | ||
const token = authorization.split(" ")[1]; | ||
console.log("Token:", token); | ||
const decoded = jwt.verify(token, process.env.JWTSecret); | ||
const database = client.db('vehicleDB'); | ||
const users = database.collection('users'); | ||
const user = await users.findOne({ email: decoded.email }); | ||
console.log(user); | ||
if (user) { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(user) | ||
}; | ||
} else { | ||
if (!decoded) { | ||
return { | ||
statusCode: 404, | ||
body: JSON.stringify({ message: 'User not found' }) | ||
statusCode: 401, | ||
body: JSON.stringify({ message: "Token invalid" }) | ||
}; | ||
} | ||
} catch (err) { | ||
const database = client.db("vehicleDB"); | ||
const users = database.collection("users"); | ||
return await users.findOne({email: decoded.email}); | ||
}).then(user => { | ||
return { | ||
statusCode: 401, | ||
body: JSON.stringify({ message: 'Unauthorized' }) | ||
statusCode: 200, | ||
body: JSON.stringify(user) | ||
}; | ||
} | ||
} | ||
} catch (err) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: `Internal Server Error: ${err.message}` }) | ||
}; | ||
} finally { | ||
await client.close(); | ||
} | ||
}; | ||
}).catch(err => { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: "Internal Server Error" }) | ||
}; | ||
}).finally(() => client.close()); | ||
}; |
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,6 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"dotenv": "^16.4.5", | ||
"jsonwebtoken": "^9.0.2", | ||
"mongodb": "^6.5.0" | ||
} | ||
} |
Oops, something went wrong.