-
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 pull request #49 from Gather307/user-auth
Merges User Auth into Main
- Loading branch information
Showing
16 changed files
with
1,364 additions
and
484 deletions.
There are no files selected for viewing
92 changes: 46 additions & 46 deletions
92
.github/workflows/azure-static-web-apps-thankful-tree-04ab28e1e.yml
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,46 +1,46 @@ | ||
name: Azure Static Web Apps CI/CD | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
types: [opened, synchronize, reopened, closed] | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build_and_deploy_job: | ||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') | ||
runs-on: ubuntu-latest | ||
name: Build and Deploy Job | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: true | ||
lfs: false | ||
- name: Build And Deploy | ||
id: builddeploy | ||
uses: Azure/static-web-apps-deploy@v1 | ||
with: | ||
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_THANKFUL_TREE_04AB28E1E }} | ||
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments) | ||
action: "upload" | ||
###### Repository/Build Configurations - These values can be configured to match your app requirements. ###### | ||
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig | ||
app_location: "./frontend" # App source code path | ||
api_location: "" # Api source code path - optional | ||
output_location: "build" # Built app content directory - optional | ||
###### End of Repository/Build Configurations ###### | ||
|
||
close_pull_request_job: | ||
if: github.event_name == 'pull_request' && github.event.action == 'closed' | ||
runs-on: ubuntu-latest | ||
name: Close Pull Request Job | ||
steps: | ||
- name: Close Pull Request | ||
id: closepullrequest | ||
uses: Azure/static-web-apps-deploy@v1 | ||
with: | ||
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_THANKFUL_TREE_04AB28E1E }} | ||
action: "close" | ||
name: Azure Static Web Apps CI/CD | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
types: [opened, synchronize, reopened, closed] | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build_and_deploy_job: | ||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') | ||
runs-on: ubuntu-latest | ||
name: Build and Deploy Job | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: true | ||
lfs: false | ||
- name: Build And Deploy | ||
id: builddeploy | ||
uses: Azure/static-web-apps-deploy@v1 | ||
with: | ||
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_THANKFUL_TREE_04AB28E1E }} | ||
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments) | ||
action: "upload" | ||
###### Repository/Build Configurations - These values can be configured to match your app requirements. ###### | ||
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig | ||
app_location: "./frontend" # App source code path | ||
api_location: "" # Api source code path - optional | ||
output_location: "build" # Built app content directory - optional | ||
###### End of Repository/Build Configurations ###### | ||
|
||
close_pull_request_job: | ||
if: github.event_name == 'pull_request' && github.event.action == 'closed' | ||
runs-on: ubuntu-latest | ||
name: Close Pull Request Job | ||
steps: | ||
- name: Close Pull Request | ||
id: closepullrequest | ||
uses: Azure/static-web-apps-deploy@v1 | ||
with: | ||
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_THANKFUL_TREE_04AB28E1E }} | ||
action: "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
node_modules/ | ||
node_modules* | ||
.vercel | ||
.env* | ||
|
||
|
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,87 @@ | ||
import bcrypt from "bcrypt"; | ||
import jwt from "jsonwebtoken"; | ||
import User, { IUser } from "./models/userSchema"; | ||
import dotenv from "dotenv"; | ||
import { Request, Response } from "express"; | ||
import connectDB from "./connection"; | ||
|
||
dotenv.config(); | ||
|
||
type User = { username: string; hashedPassword: string }; | ||
const creds = [User]; // username, hashedPassword | ||
|
||
export function authenticateUser(req: Request, res: Response, next: any) { | ||
const authHeader = req.headers["authorization"]; | ||
//Getting the 2nd part of the auth header (the token) | ||
const token = authHeader && authHeader.split(" ")[1]; | ||
|
||
if (!token) { | ||
console.log("No token received"); | ||
res.status(401).end(); | ||
} else { | ||
jwt.verify( | ||
token, | ||
process.env.TOKEN_SECRET as jwt.Secret, | ||
(error, decoded) => { | ||
if (decoded) { | ||
next(); | ||
} else { | ||
console.log("JWT error:", error); | ||
res.status(401).end(); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
|
||
export const loginUser = async (req: Request, res: Response) => { | ||
connectDB(); | ||
const { username, password } = req.body; // from form | ||
const existingUser = await User.findOne({ username }).orFail(); | ||
console.log("Existing user:", existingUser); | ||
|
||
if (existingUser == null) { | ||
// invalid username | ||
res.status(401).send("Unauthorized: Not a user"); | ||
} else { | ||
try { | ||
console.log("Comparing passwords"); | ||
console.log(password, existingUser.password); | ||
const matched = await bcrypt.compare(password, existingUser.password); | ||
console.log("Password matched:", matched); | ||
if (matched) { | ||
const token = await generateAccessToken(username); | ||
console.log("Token generated:", token); | ||
res.status(200).send({ existingUser, token }); | ||
} else { | ||
// invalid password | ||
console.log("Invalid password"); | ||
res.status(401).send("Unauthorized: Invalid password"); | ||
} | ||
} catch (error) { | ||
console.log("Failed to authenticate user"); | ||
res.status(401).send("Unauthorized: Failed to authenticate user"); | ||
} | ||
} | ||
}; | ||
|
||
function generateAccessToken(username: any) { | ||
return new Promise((resolve, reject) => { | ||
jwt.sign( | ||
{ username: username }, | ||
process.env.TOKEN_SECRET as jwt.Secret, | ||
{ expiresIn: "1d" }, | ||
(error: Error | null, token: string | undefined) => { | ||
if (error) { | ||
reject(error); | ||
} else if (token) { | ||
resolve(token); | ||
} else { | ||
reject(new Error("Token generation failed")); | ||
} | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
export { generateAccessToken }; |
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
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 |
---|---|---|
|
@@ -7,7 +7,6 @@ yarn-error.log* | |
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
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
Oops, something went wrong.