Skip to content

Commit

Permalink
Merge pull request #33 from oslabs-beta/PostgreSQL
Browse files Browse the repository at this point in the history
Postgres sql
  • Loading branch information
bryentsariwati authored Apr 25, 2023
2 parents 192db7e + 8714ec8 commit 1f89784
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions client/src/pages/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Signup = () => {
const userData = {
email,
password,
fullName,
full_name: fullName,
};
console.log(userData);
fetch('/createUser', {
Expand All @@ -31,7 +31,7 @@ const Signup = () => {
})
.then((response) => {
console.log(response);
if (response.status === 200) {
if (response.status === 201) {
navigate('/dashboard');
} else if (response.status === 409) {
setErrorMessage('Email already exists');
Expand Down
18 changes: 10 additions & 8 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const bcrypt = require('bcrypt');
const { query } = require('../db.config.js');
const jwt = require("jsonwebtoken");
const jwt = require('jsonwebtoken');

// Creation of user using PostgresSQL

const createUser = async (req, res, next) => {
const { full_name, email, password } = req.body;
console.log('in create user');
console.log(full_name, email, password);
try {
const getResult = await query('SELECT * FROM users WHERE email = $1', [
Expand All @@ -17,13 +18,14 @@ const createUser = async (req, res, next) => {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);

await query(
'INSERT INTO users (full_name, email, password) VALUES ($1,$2,$3)',
const result = await query(
'INSERT INTO users (full_name, email, password) VALUES ($1, $2, $3) RETURNING _id',
[full_name, email, hashedPassword]
);
const userId = result.rows[0].id;

console.log('user created successfully');
res.locals.userId = userData._id;
res.locals.userId = userId;

return next();
} catch (err) {
Expand Down Expand Up @@ -66,18 +68,18 @@ const verifyUser = async (req, res, next) => {
}
};

const logout = (req,res,next) => {
const logout = (req, res, next) => {
try {
res.clearCookie("token");
res.clearCookie('token');
res.sendStatus(200);
} catch(err) {
} catch (err) {
console.log('Error', err);
let error = {
log: 'Express error handler caught userController.verifyUser',
message: { err: err },
};
return next(error);
}
}
};

module.exports = { createUser, verifyUser, logout };

0 comments on commit 1f89784

Please sign in to comment.