Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge auth into main #39

Merged
merged 28 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d560285
created catalog table
Nov 18, 2023
8b92d59
AT - created the published schedule table
arralia Nov 19, 2023
a499026
fixed enum name
arralia Nov 19, 2023
1155340
removed brackets around exist conditionals
arralia Nov 19, 2023
0e83312
added create table users statement
Nov 19, 2023
e2e0b73
added drop table if exists
Nov 19, 2023
19035e2
updated drop table statement
Nov 19, 2023
d68a4d2
Made CRUD SQL queries for Published Schedule Table
SubinQKim Nov 20, 2023
847c958
added catalog CRUD queries
chloecheng8 Nov 21, 2023
589e537
Attempt to figure out ENV variable issue with workflows
leeaj8-uci Nov 22, 2023
9f350b0
fix typo
leeaj8-uci Nov 22, 2023
559dab7
try moving env variables
leeaj8-uci Nov 22, 2023
f810b9d
Revert changes
leeaj8-uci Nov 22, 2023
34eff36
Merge pull request #14 from ctc-uci/8-create-catalog-table
ThatMegamind Nov 22, 2023
80ac134
Merge pull request #15 from ctc-uci/7-create-published-schedule-table
ThatMegamind Nov 22, 2023
fe99a58
Merge pull request #16 from ctc-uci/9-create-user-table
ThatMegamind Nov 22, 2023
5ad36f1
Merge pull request #17 from ctc-uci/10-make-crud-sql-queries-for-publ…
ThatMegamind Nov 22, 2023
32a94b0
Merge pull request #18 from ctc-uci/11-make-crud-sql-queries-for-cata…
ThatMegamind Nov 23, 2023
c042554
Make CRUD SQL queries for Users Table (#25)
ctc-devops Nov 25, 2023
57cc996
fixed published schedule types
michellelin1 Nov 27, 2023
8c703e9
added connection to db
michellelin1 Nov 27, 2023
15d3d25
23-make-backend-routes-for-users (#26)
ctc-devops Dec 18, 2023
a2965f7
Make backend routes for Catelog (#27)
ctc-devops Dec 18, 2023
e6e02f1
Set up the nodeMailer route and the transporter (#28)
ctc-devops Dec 30, 2023
699f437
Make Backend Routes for Published Schedule (#29)
ctc-devops Jan 3, 2024
39cccb0
Minor DB Updates (#32)
ctc-devops Jan 8, 2024
1e5863b
Protected routes have token refresh method, all CRUD methods work wit…
ctc-devops Jan 23, 2024
50abe67
merged main into dev
michellelin1 Jan 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ dist
# DynamoDB Local files
.dynamodb/

# firebase sdk
firebase-adminsdk.json

# TernJS port file
.tern-port

Expand Down
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require('dotenv').config();
// routes
const users = require('./routes/users');

const { authRouter } = require('./routes/auth');

const email = require('./routes/nodeMailer');

const app = express();
Expand All @@ -30,6 +32,7 @@ app.use('/published-schedule', publishedScheduleRouter);
app.use('/users', users);
app.use('/catalog', catalogRouter);
app.use('/nodeMailer', email);
app.use('/auth', authRouter);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
9 changes: 9 additions & 0 deletions firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const admin = require('firebase-admin');

require('dotenv').config();

const credentials = require('./firebase-adminsdk.json');
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved

admin.initializeApp({ credential: admin.credential.cert(credentials) });

module.exports = admin;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"eslint-plugin-prettier": "^4.0.0",
"express": "^4.17.1",
"express-promise-router": "^4.1.1",
"firebase-admin": "^12.0.0",
"nodemailer": "^6.9.7",
"nodemon": "^2.0.14",
"pg": "^8.8.0",
Expand Down
41 changes: 41 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const express = require('express');

const authRouter = express();
const admin = require('../firebase');

authRouter.use(express.json());

// This method makes a call to Firebase that will verify the access token attached to the request's cookies
// This method is used to make sure that only users who have appropriate access tokens can access backend routes.
const verifyToken = async (req, res, next) => {
try {
const {
cookies: { accessToken },
} = req;
if (!accessToken) {
return res.status(400).send('@verifyToken no access token');
}
const decodedToken = await admin.auth().verifyIdToken(accessToken);
if (!decodedToken) {
return res.status(400).send('Empty token from firebase');
}
return next();
} catch (err) {
return res.status(400).send('@verifyToken no access token');
}
};

// This method makes a call to firebase that will verify the access token attached to the request's cookies
// This method is used to make sure that only users who have appropriate access tokens can access frontend routes.
authRouter.get('/verifyToken/:accessToken', async (req, res) => {
try {
const { accessToken } = req.params;
const decodedToken = await admin.auth().verifyIdToken(accessToken);
return res.status(200).send(decodedToken.uid);
} catch (err) {
console.log('err', err);
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
return res.status(400).send('@verifyToken no access token');
}
});

module.exports = { verifyToken, authRouter };
2 changes: 0 additions & 2 deletions routes/nodeMailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ emailRouter.use(express.json());

emailRouter.post('/send', (req, res) => {
const { email, messageHtml, subject } = req.body;
console.log('req.body', req.body);
console.log('email', email);
const mail = {
from: `${process.env.REACT_APP_EMAIL_FIRST_NAME} ${process.env.REACT_APP_EMAIL_LAST_NAME} ${process.env.REACT_APP_EMAIL_USERNAME}`,
to: email,
Expand Down
22 changes: 20 additions & 2 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { db } = require('../server/db');

const userRouter = express.Router();

const admin = require('../firebase');

userRouter.get('/', async (req, res) => {
try {
const allUsers = await db.query(`SELECT * FROM users;`);
Expand All @@ -13,6 +15,17 @@ userRouter.get('/', async (req, res) => {
}
});

// logInWithEmailAndPassword() needs to get specific user id
userRouter.get('/:uid', async (req, res) => {
try {
const { uid } = req.params;
const user = await db.query(`SELECT * FROM users WHERE id = $1;`, [uid]);
res.status(200).json(keysToCamel(user));
} catch (err) {
res.status(500).send(err.message);
}
});

userRouter.get('/pending-accounts', async (req, res) => {
try {
const pendingAccounts = await db.query(`SELECT * FROM users WHERE approved = FALSE;`);
Expand All @@ -22,7 +35,7 @@ userRouter.get('/pending-accounts', async (req, res) => {
}
});

userRouter.post('/', async (req, res) => {
userRouter.post('/create', async (req, res) => {
try {
const { id, email, type, approved } = req.body;
await db.query(`INSERT INTO users (id, email, "type", approved) VALUES ($1, $2, $3, $4);`, [
Expand All @@ -35,14 +48,15 @@ userRouter.post('/', async (req, res) => {
id,
});
} catch (err) {
console.log('err', err);
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
res.status(500).json({
status: 'Failed',
msg: err.message,
});
}
});

userRouter.put('/:uid', async (req, res) => {
userRouter.put('/approve/:uid', async (req, res) => {
try {
const { uid } = req.params;
const updatedApproval = await db.query(
Expand All @@ -58,6 +72,10 @@ userRouter.put('/:uid', async (req, res) => {
userRouter.delete('/:uid', async (req, res) => {
try {
const { uid } = req.params;

// Firebase delete
await admin.auth().deleteUser(uid);

const deletedUser = await db.query(`DELETE FROM users WHERE id = $1 RETURNING *;`, [uid]);
res.status(200).send(keysToCamel(deletedUser));
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions transporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const nodemailer = require('nodemailer');
require('dotenv').config();

// sender information

const transport = {
host: 'smtp.gmail.com', // e.g. smtp.gmail.com
auth: {
Expand Down
Loading
Loading