From 80f29a1d01cb0c1bc9341576834081b7162d2d92 Mon Sep 17 00:00:00 2001 From: Victorv <v.vogt@etu.unistra.fr> Date: Sun, 2 Jan 2022 17:29:28 +0100 Subject: [PATCH] :sparkles: comments + clean non used code --- backend/models/user.js | 5 - backend/routes/auth.js | 10 +- frontend/src/actions/Login.actions.js | 99 -------------------- frontend/src/actions/Login.actions.js.BASE | 92 ------------------ frontend/src/actions/Login.actions.js.LOCAL | 99 -------------------- frontend/src/actions/Login.actions.js.REMOTE | 93 ------------------ frontend/src/containers/Bank.container.js | 9 -- 7 files changed, 8 insertions(+), 399 deletions(-) delete mode 100644 frontend/src/actions/Login.actions.js delete mode 100644 frontend/src/actions/Login.actions.js.BASE delete mode 100644 frontend/src/actions/Login.actions.js.LOCAL delete mode 100644 frontend/src/actions/Login.actions.js.REMOTE diff --git a/backend/models/user.js b/backend/models/user.js index ad6965b..c3c8557 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -72,11 +72,6 @@ UserSchema.plugin(uniqueValidator); UserSchema.pre('save', function(next) { let user = this; - /*if (!user.isModified('password')) { - return next(); - } else { - }*/ - bcrypt .genSalt(12) .then((salt) => { diff --git a/backend/routes/auth.js b/backend/routes/auth.js index a4a5a03..00c0080 100644 --- a/backend/routes/auth.js +++ b/backend/routes/auth.js @@ -6,12 +6,14 @@ const jwt = require('jsonwebtoken'); const secret = process.env.SECRET || 'some other secret as default'; const passport = require('passport'); +// Headers for accepting request from all endpoints router.options('*', async (req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); +// Request for signup router.post('/signup', async (req, res) => { var errors = {}; const user = await User.findOne({username: req.body.username}); @@ -37,6 +39,7 @@ router.post('/signup', async (req, res) => { return res.status(200).json({}); }); +// Request for login router.post('/login', async (req, res) => { const errors = {}; const username = req.body.username @@ -50,8 +53,7 @@ router.post('/login', async (req, res) => { } isMatch = await bcrypt.compare(password, user.password); - //isMatch = password == user.password; - // return 400 if password does not match + if (!isMatch) { errors.message = "Password is incorrect"; return res.status(400).json(errors); @@ -76,6 +78,7 @@ router.post('/login', async (req, res) => { token: `Bearer ${token}` }); }); +// Request for paid adhesion router.post('/pay_adhesion', async (req, res) => { const student_number = req.body.student_number; @@ -101,12 +104,14 @@ router.post('/pay_adhesion', async (req, res) => { } }); +// Request for connected account infos router.get('/me', passport.authenticate('jwt', {session: false}), async function(req, res, next) { const username = req.user.username; const dbUser = await User.findOne({ username }); return res.status(200).json(dbUser); }); +// Request for updating connected account infos router.post('/me/update', passport.authenticate('jwt', {session: false}), async function(req, res, next) { const username = req.user.username; const firstname = req.body.firstname; @@ -157,6 +162,7 @@ router.post('/me/update', passport.authenticate('jwt', {session: false}), async res.status(200).json(); }); +// Request for all created student accounts router.get('/etudiants', async (req, res) => { const users = await User.find(); res.header("Access-Control-Allow-Origin", "*"); diff --git a/frontend/src/actions/Login.actions.js b/frontend/src/actions/Login.actions.js deleted file mode 100644 index 7f2a953..0000000 --- a/frontend/src/actions/Login.actions.js +++ /dev/null @@ -1,99 +0,0 @@ -import { userService } from "../services/authentication.service"; -import {fetchUserData} from "./Profile.actions"; - -export const getAuth = () => { - return { - type:'GET_AUTH' - } -} - -export const logoutSuccess = () => { - return { - type:'LOGOUT_SUCCESS' - } -} - -export const loginSuccessWaitForPayment = () => { - return { - type:'LOGIN_SUCCESS_WAIT_PAYMENT' - } -} - -export const loginSuccesPaymentDone = () => { - return { - type:'LOGIN_SUCCESS_PAYMENT_DONE' - } -} - -export const loginFailed = (message) => { - return { - type:'LOGIN_FAILED', - message: message - } -} - -export const loginRequest = () => { - return { - type:'LOGIN_REQUEST' - } -} - -export const login = (loginData, ownProps) => { - return async (dispatch) => { - dispatch(loginRequest()); - - const response = await fetch( "/api/login", { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(loginData), - }) - - if(response.ok){ - response.json().then(data => { - userService.setToken(data.token); - let user = dispatch(fetchUserData()); - //dispatch(user); - ownProps.history.push('/'); - - - // TODO: do another request to know if user paid adhesion - // make request to get user data - - - var paid=true; - - - if(paid){ - userService.setAdhesion(true); - dispatch(loginSuccesPaymentDone(data)); - } else { - userService.setToken(false); - dispatch(loginSuccessWaitForPayment(data)); - } - }).catch(err => dispatch(loginFailed(err))); - } - else{ - response.json().then(error => { - dispatch(loginFailed(error)); - }).catch(err => dispatch(loginFailed(err))); - } - - return response; - } -} - -export const logout = () => { - return (dispatch) => { - userService.logout(); - dispatch(logoutSuccess()); - } -} - -export const reinitializeState = () => { - return { - type:'REINITIALIZE_STATE' - } -} diff --git a/frontend/src/actions/Login.actions.js.BASE b/frontend/src/actions/Login.actions.js.BASE deleted file mode 100644 index 44c4a25..0000000 --- a/frontend/src/actions/Login.actions.js.BASE +++ /dev/null @@ -1,92 +0,0 @@ -import { userService } from "../services/authentication.service"; -import {fetchUserData} from "./Profile.actions"; - -export const getAuth = () => { - return { - type:'GET_AUTH' - } -} - -export const logoutSuccess = () => { - return { - type:'LOGOUT_SUCCESS' - } -} - -export const loginSuccessWaitForPayment = () => { - return { - type:'LOGIN_SUCCESS_WAIT_PAYMENT' - } -} - -export const loginSuccesPaymentDone = () => { - return { - type:'LOGIN_SUCCESS_PAYMENT_DONE' - } -} - -export const loginFailed = (message) => { - return { - type:'LOGIN_FAILED', - message: message - } -} - -export const loginRequest = () => { - return { - type:'LOGIN_REQUEST' - } -} - -export const login = (loginData, ownProps) => { - return async (dispatch) => { - dispatch(loginRequest()); - - const response = await fetch( "/api/login", { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(loginData), - }) - - if(response.ok){ - response.json().then(data => { - userService.setToken(data.token); - dispatch(fetchUserData()); - ownProps.history.push('/'); - // TODO: do another request to know if user paid adhesion - var paid=true; - if(paid){ - userService.setAdhesion(true); - dispatch(loginSuccesPaymentDone(data)); - } else { - userService.setToken(false); - dispatch(loginSuccessWaitForPayment(data)); - } - }).catch(err => dispatch(loginFailed(err))); - } - else{ - response.json().then(error => { - dispatch(loginFailed(error)); - }).catch(err => dispatch(loginFailed(err))); - } - - return response; - } -} - -export const logout = () => { - return (dispatch) => { - userService.logout(); - dispatch(logoutSuccess()); - } -} - -export const reinitializeState = () => { - return { - type:'REINITIALIZE_STATE' - } -} - diff --git a/frontend/src/actions/Login.actions.js.LOCAL b/frontend/src/actions/Login.actions.js.LOCAL deleted file mode 100644 index 23ba633..0000000 --- a/frontend/src/actions/Login.actions.js.LOCAL +++ /dev/null @@ -1,99 +0,0 @@ -import { userService } from "../services/authentication.service"; -import {fetchUserData} from "./Profile.actions"; - -export const getAuth = () => { - return { - type:'GET_AUTH' - } -} - -export const logoutSuccess = () => { - return { - type:'LOGOUT_SUCCESS' - } -} - -export const loginSuccessWaitForPayment = () => { - return { - type:'LOGIN_SUCCESS_WAIT_PAYMENT' - } -} - -export const loginSuccesPaymentDone = () => { - return { - type:'LOGIN_SUCCESS_PAYMENT_DONE' - } -} - -export const loginFailed = (message) => { - return { - type:'LOGIN_FAILED', - message: message - } -} - -export const loginRequest = () => { - return { - type:'LOGIN_REQUEST' - } -} - -export const login = (loginData, ownProps) => { - return async (dispatch) => { - dispatch(loginRequest()); - - const response = await fetch( "/api/login", { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(loginData), - }) - - if(response.ok){ - response.json().then(data => { - userService.setToken(data.token); - dispatch(fetchUserData()); - ownProps.history.push('/'); - - - // TODO: do another request to know if user paid adhesion - // make request to get user data - - - var paid=true; - - - if(paid){ - userService.setAdhesion(true); - dispatch(loginSuccesPaymentDone(data)); - } else { - userService.setToken(false); - dispatch(loginSuccessWaitForPayment(data)); - } - }).catch(err => dispatch(loginFailed(err))); - } - else{ - response.json().then(error => { - dispatch(loginFailed(error)); - }).catch(err => dispatch(loginFailed(err))); - } - - return response; - } -} - -export const logout = () => { - return (dispatch) => { - userService.logout(); - dispatch(logoutSuccess()); - } -} - -export const reinitializeState = () => { - return { - type:'REINITIALIZE_STATE' - } -} - diff --git a/frontend/src/actions/Login.actions.js.REMOTE b/frontend/src/actions/Login.actions.js.REMOTE deleted file mode 100644 index 967ae30..0000000 --- a/frontend/src/actions/Login.actions.js.REMOTE +++ /dev/null @@ -1,93 +0,0 @@ -import { userService } from "../services/authentication.service"; -import {fetchUserData} from "./Profile.actions"; - -export const getAuth = () => { - return { - type:'GET_AUTH' - } -} - -export const logoutSuccess = () => { - return { - type:'LOGOUT_SUCCESS' - } -} - -export const loginSuccessWaitForPayment = () => { - return { - type:'LOGIN_SUCCESS_WAIT_PAYMENT' - } -} - -export const loginSuccesPaymentDone = () => { - return { - type:'LOGIN_SUCCESS_PAYMENT_DONE' - } -} - -export const loginFailed = (message) => { - return { - type:'LOGIN_FAILED', - message: message - } -} - -export const loginRequest = () => { - return { - type:'LOGIN_REQUEST' - } -} - -export const login = (loginData, ownProps) => { - return async (dispatch) => { - dispatch(loginRequest()); - - const response = await fetch( "/api/login", { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(loginData), - }) - - if(response.ok){ - response.json().then(data => { - userService.setToken(data.token); - let user = dispatch(fetchUserData()); - //dispatch(user); - ownProps.history.push('/'); - // TODO: do another request to know if user paid adhesion - console.log(user) - var paid=true; - if(paid){ - userService.setAdhesion(true); - dispatch(loginSuccesPaymentDone(data)); - } else { - userService.setToken(false); - dispatch(loginSuccessWaitForPayment(data)); - } - }).catch(err => dispatch(loginFailed(err))); - } - else{ - response.json().then(error => { - dispatch(loginFailed(error)); - }).catch(err => dispatch(loginFailed(err))); - } - - return response; - } -} - -export const logout = () => { - return (dispatch) => { - userService.logout(); - dispatch(logoutSuccess()); - } -} - -export const reinitializeState = () => { - return { - type:'REINITIALIZE_STATE' - } -} diff --git a/frontend/src/containers/Bank.container.js b/frontend/src/containers/Bank.container.js index 751bb01..eb01093 100644 --- a/frontend/src/containers/Bank.container.js +++ b/frontend/src/containers/Bank.container.js @@ -14,18 +14,9 @@ export class Bank extends React.Component { super(props); } - componentDidUpdate(){ - console.log('PROFILE', this.props.profile); - console.log('BANK', this.props.bank); - console.log('PROPS', this.props); - } - componentDidMount(){ this.props.fetchUserData(); this.props.fetchUserTransactions(this.props.profile.me.student_number); - console.log('MOUNT PROFILE', this.props.profile); - console.log('MOUNT BANK', this.props.bank); - console.log('MOUNT PROPS', this.props); } convertDate(formated_Date){ -- GitLab