Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 4 additions & 9 deletions server/config/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@ const verify = async (accessToken, refreshToken, profile, callback) => {
}

try {
const results = await pool.query('SELECT * FROM users WHERE user_metadata->>\'username\' = $1', [userData.username])
const results = await pool.query('SELECT * FROM users WHERE username = $1', [userData.username])
const user = results.rows[0]

if (!user) {
const newResults = await pool.query(
`INSERT INTO users (user_metadata)
VALUES ($1)
`INSERT INTO users (github_id, username, avatar_url)
VALUES ($1, $2, $3)
RETURNING *`,
[JSON.stringify({
githubId: userData.githubId,
username: userData.username,
avatarUrl: userData.avatarUrl,
accessToken: accessToken
})]
[userData.githubId, userData.username, userData.avatarUrl]
)

const newUser = newResults.rows[0]
Expand Down
5 changes: 3 additions & 2 deletions server/config/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const dropTables = async () => {
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS profiles;
DROP TABLE IF EXISTS hashtags;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS auth.users;
`;

Expand All @@ -39,6 +40,8 @@ const createUsersTable = async () => {
email TEXT UNIQUE,
encrypted_password TEXT,
github_id TEXT UNIQUE,
username TEXT UNIQUE,
avatar_url TEXT,
provider TEXT DEFAULT 'github',
created_at TIMESTAMP DEFAULT now()
);
Expand Down Expand Up @@ -93,10 +96,8 @@ const createProfilesTable = async () => {
CREATE TABLE IF NOT EXISTS profiles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id),
username TEXT UNIQUE,
bio TEXT,
location TEXT,
avatar_url TEXT,
links TEXT,
created_at TIMESTAMP DEFAULT now()
);
Expand Down
9 changes: 9 additions & 0 deletions server/middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const isAuthenticated = (req, res, next) => {
if (!req.isAuthenticated || !req.isAuthenticated() || !req.user) {
return res.status(401).json({ error: 'You must be logged in to perform this action.' })
}

next()
}

export default isAuthenticated
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "concurrently \"cd client && vite\" \"cd server && nodemon --require dotenv/config server.js\"",
"dev": "nodemon server.js",
"reset": "node config/reset.js",
"start": "npm run reset && node server/server.js",
"build": "cd client && vite build"
Expand Down
9 changes: 4 additions & 5 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import './config/dotenv.js'
import express from 'express'
import cors from 'cors'
import passport from 'passport'
import session from 'express-session'
import './config/dotenv.js'
import { GitHub } from './config/auth.js'
import authRoutes from './routes/auth.js'

Expand All @@ -15,18 +15,17 @@ if (isProduction) {
app.set('trust proxy', 1)
}

app.use(express.json())

app.use(
cors({
origin: process.env.CLIENT_URL || 'http://localhost:5173',
methods: 'GET,POST,PUT,DELETE,PATCH',
credentials: true,
})
)
app.use(express.json())

app.use(session({
secret: process.env.SESSION_SECRET || 'codepath-dev-secret',
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
Expand All @@ -50,5 +49,5 @@ passport.deserializeUser((user, done) => {
app.use('/auth', authRoutes)

app.listen(PORT, () => {
console.log(`server running on http://localhost:${PORT}`)
console.log(`Server running on http://localhost:${PORT}`)
})