| @@ -1,11 +1,64 @@ | ||
| import React from "react"; | ||
| import { Redirect, Route, Link } from 'react-router-dom' | ||
| // const Nav = () => ( | ||
| // <nav className="navbar navbar-expand-lg navbar-dark bg-primary"> | ||
| // <a className="navbar-brand" href="/"> | ||
| // React Reading List | ||
| // </a> | ||
| // </nav> | ||
| // ); | ||
|
|
||
|
|
||
| const Nav = props => { | ||
| if (props.loggedIn) { | ||
| return ( | ||
| <nav className="navbar"> | ||
| <ul className="nav"> | ||
| <li className="nav-item"> | ||
| <Link to="/TeacherPortal" className="nav-link"> | ||
| Teacher Portal | ||
| </Link> | ||
| </li> | ||
| <li> | ||
| <Link to="#" className="nav-link" onClick={props._logout}> | ||
| Logout | ||
| </Link> | ||
| </li> | ||
| </ul> | ||
| </nav> | ||
| ) | ||
| } else { | ||
| return ( | ||
| <nav className="navbar"> | ||
| <ul className="nav"> | ||
| <li className="nav-item"> | ||
| <Link to="/login" className="nav-link"> | ||
| Guardian Login | ||
| </Link> | ||
| </li> | ||
| <li className="nav-item"> | ||
| <Link to="/teacherlogin" className="nav-link"> | ||
| Teacher Login | ||
| </Link> | ||
| </li> | ||
| <li className="nav-item"> | ||
| <Link to="/teacherSignup" className="nav-link"> | ||
| Teacher Signup | ||
| </Link> | ||
| </li> | ||
| <li className="nav-item"> | ||
| <Link to="/guardianSignup" className="nav-link"> | ||
| Guardian Signup | ||
| </Link> | ||
| </li> | ||
| </ul> | ||
| </nav> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| export default Nav; |
| @@ -0,0 +1,10 @@ | ||
| import React from 'react'; | ||
| import Webcam from 'react-webcam'; | ||
|
|
||
| class Camera extends React.Component { | ||
| render() { | ||
| return <Webcam/>; | ||
| } | ||
| } | ||
|
|
||
| export default Camera; |
| @@ -0,0 +1 @@ | ||
| export { default } from "./Camera.js"; |
| @@ -1,8 +1,12 @@ | ||
| import React from 'react' | ||
| import ReactDOM from 'react-dom' | ||
| import App from './App' | ||
| import './index.css' | ||
| import { BrowserRouter } from 'react-router-dom' | ||
|
|
||
| ReactDOM.render( | ||
| <BrowserRouter> | ||
| <App /> | ||
| </BrowserRouter>, | ||
| document.getElementById('root') | ||
| ) |
| @@ -1,41 +1,142 @@ | ||
| import React, { Component } from 'react' | ||
| import { Redirect } from 'react-router-dom' | ||
|
|
||
| // import React, { Component } from "react"; | ||
| import { Col, Row, Container } from "../../components/Grid"; | ||
| import API from "../../utils/API"; | ||
| import { Input, FormBtn} from "../../components/Form"; | ||
|
|
||
| class Login extends Component { | ||
| constructor() { | ||
| super() | ||
| this.state = { | ||
| // username: '', | ||
| email: '', | ||
| password: '', | ||
| redirectTo: null | ||
| } | ||
|
|
||
| this.handleSubmit = this.handleSubmit.bind(this) | ||
| this.handleChange = this.handleChange.bind(this) | ||
| } | ||
|
|
||
| handleChange(event) { | ||
| this.setState({ | ||
| [event.target.name]: event.target.value | ||
| }) | ||
| } | ||
|
|
||
| handleSubmit(event) { | ||
| event.preventDefault() | ||
| console.log(this.state.email) | ||
| // console.log(this.state.redirectTo) | ||
| this.props._login(this.state.email, this.state.password) | ||
| this.setState({ | ||
| redirectTo: '/Temp', | ||
| loggedIn: true, | ||
|
|
||
| }) | ||
| // .then (<Redirect to={"/Temp"} />) | ||
|
|
||
| } | ||
|
|
||
| render() { | ||
|
|
||
| if (this.state.loggedIn) { | ||
| return <Redirect to="/Temp" /> | ||
| console.log(this.state) | ||
| } else { | ||
| return ( | ||
| <div className="Login"> | ||
| <h1>Guardian Login</h1> | ||
| <form> | ||
| <label htmlFor="email">Email: </label> | ||
| <input | ||
| type="text" | ||
| name="email" | ||
| value={this.state.email} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="password">Password: </label> | ||
| <input | ||
| type="password" | ||
| name="password" | ||
| value={this.state.password} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <button onClick={this.handleSubmit}>Login</button> | ||
| </form> | ||
|
|
||
| </div> | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| // state = { | ||
| // familyName: "", | ||
| // name: "" | ||
| // }; | ||
|
|
||
|
|
||
| // handleInputChange = event => { | ||
| // const { name, value } = event.target; | ||
| // this.setState({ | ||
| // [name]: value | ||
| // }); | ||
| // }; | ||
|
|
||
| // handleAddFamily = event => { | ||
| // event.preventDefault(); | ||
| // API.addFamily({ | ||
| // familyName: this.state.familyName | ||
| // }) | ||
| // }; | ||
|
|
||
|
|
||
|
|
||
| <<<<<<< HEAD | ||
| return ( | ||
| <Container fluid> | ||
| <h1>Hello World</h1> | ||
| </Container> | ||
| ) | ||
| } | ||
| }; | ||
| ======= | ||
| // render() { | ||
| >>>>>>> ef02c4f79c161b5548fe408cece6740cdf577713 | ||
|
|
||
| // return ( | ||
| // <Container fluid> | ||
| // <h1>Hello World</h1> | ||
| // <div className="panel panel-default"> | ||
| // <div className="panel-body"> | ||
| // <Row> | ||
| // <Col size="md-6"> | ||
| // <div className="form-group"> | ||
| // <Input className="form-control" | ||
| // value={this.state.familyName} | ||
| // onChange={this.handleInputChange} | ||
| // name="fName" | ||
| // placeholder="Enter Family Name" | ||
| // /> | ||
| // </div> | ||
| // </Col> | ||
| // </Row> | ||
| // <Row> | ||
| // <Col size="md-6"> | ||
| // <FormBtn | ||
| // onClick={this.handleAddFamily} | ||
| // > | ||
| // Input Family | ||
| // </FormBtn> | ||
| // </Col> | ||
| // </Row> | ||
| // </div> | ||
| // </div> | ||
| // </Container> | ||
| // ) | ||
| // } | ||
| // }; | ||
|
|
||
| export default Login |
| @@ -1 +1 @@ | ||
| export { default } from "./Login.js"; |
| @@ -0,0 +1,34 @@ | ||
| import React from 'react'; | ||
| import Webcam from 'react-webcam'; | ||
|
|
||
| class TestWebCam extends React.Component { | ||
| setRef = (webcam) => { | ||
| this.webcam = webcam; | ||
| } | ||
|
|
||
| capture = () => { | ||
| console.log("I SAW A CLICK"); | ||
| const imageSrc = this.webcam.getScreenshot(); | ||
| console.log(imageSrc); | ||
| }; | ||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <h1>I am here</h1> | ||
| <div> | ||
| <Webcam | ||
| audio={false} | ||
| height={350} | ||
| ref={this.setRef} | ||
| screenshotFormat="image/jpeg" | ||
| width={350} | ||
| /> | ||
| <button onClick={this.capture}>Capture photo</button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default TestWebCam; |
| @@ -0,0 +1 @@ | ||
| export { default } from "./TestWebCam"; |
| @@ -0,0 +1,103 @@ | ||
| import React, { Component } from 'react' | ||
| import axios from 'axios' | ||
| import { Redirect } from 'react-router-dom' | ||
|
|
||
| class guardianSignup extends Component { | ||
| constructor() { | ||
| super() | ||
| this.state = { | ||
| fName: '', | ||
| lName: '', | ||
| email: '', | ||
| phone: '', | ||
| password: '', | ||
| confirmPassword: '', | ||
| redirectTo: null | ||
| } | ||
| this.handleSubmit = this.handleSubmit.bind(this) | ||
| this.handleChange = this.handleChange.bind(this) | ||
| } | ||
| handleChange(event) { | ||
| this.setState({ | ||
| [event.target.name]: event.target.value | ||
| }) | ||
| } | ||
| handleSubmit(event) { | ||
| event.preventDefault() | ||
| // TODO - validate! | ||
| axios | ||
| .post('/auth/signup', { | ||
| fName: this.state.fName, | ||
| lName: this.state.lName, | ||
| email: this.state.email, | ||
| phone: this.state.phone, | ||
| password: this.state.password | ||
| }) | ||
| .then(response => { | ||
| console.log(response) | ||
| if (!response.data.errmsg) { | ||
| console.log('youre good') | ||
| this.setState({ | ||
| redirectTo: '/Temp' | ||
| }) | ||
| } else { | ||
| console.log('duplicate') | ||
| } | ||
| }) | ||
| } | ||
| render() { | ||
| if (this.state.redirectTo) { | ||
| return <Redirect to={{ pathname: this.state.redirectTo }} /> | ||
| } | ||
| return ( | ||
| <div className="guardianSignup"> | ||
| <h1>Guardian Signup Form</h1> | ||
| <label htmlFor="fName">Firstname: </label> | ||
| <input | ||
| type="text" | ||
| name="fName" | ||
| value={this.state.fName} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="lName">Lastname: </label> | ||
| <input | ||
| type="text" | ||
| name="lName" | ||
| value={this.state.lName} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="email">Email: </label> | ||
| <input | ||
| type="text" | ||
| name="email" | ||
| value={this.state.email} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="phone">Phone: </label> | ||
| <input | ||
| type="string" | ||
| name="phone" | ||
| value={this.state.phone} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="password">Password: </label> | ||
| <input | ||
| type="password" | ||
| name="password" | ||
| value={this.state.password} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="confirmPassword">Confirm Password: </label> | ||
| <input | ||
| type="password" | ||
| name="confirmPassword" | ||
| value={this.state.confirmPassword} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <button onClick={this.handleSubmit}>Sign up</button> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default guardianSignup |
| @@ -0,0 +1 @@ | ||
| export { default } from "./guardianSignup.js"; |
| @@ -0,0 +1 @@ | ||
| export { default } from "./teacherLogin"; |
| @@ -0,0 +1,72 @@ | ||
| import React, { Component } from 'react' | ||
| import { Redirect } from 'react-router-dom' | ||
|
|
||
|
|
||
| class teacherLogin extends Component { | ||
| constructor() { | ||
| super() | ||
| this.state = { | ||
| // username: '', | ||
| email: '', | ||
| password: '', | ||
| redirectTo: null | ||
| } | ||
|
|
||
| this.handleSubmit = this.handleSubmit.bind(this) | ||
| this.handleChange = this.handleChange.bind(this) | ||
| } | ||
|
|
||
| handleChange(event) { | ||
| this.setState({ | ||
| [event.target.name]: event.target.value | ||
| }) | ||
| } | ||
|
|
||
| handleSubmit(event) { | ||
| event.preventDefault() | ||
| console.log('handleSubmit') | ||
| // console.log(this.state.redirectTo) | ||
| this.props._teacherlogin(this.state.email, this.state.password) | ||
| this.setState({ | ||
| redirectTo: '/Temp', | ||
| loggedIn: true, | ||
|
|
||
| }) | ||
| // .then (<Redirect to={"/Temp"} />) | ||
|
|
||
| } | ||
|
|
||
| render() { | ||
|
|
||
| if (this.state.loggedIn) { | ||
| return <Redirect to="/Temp" /> | ||
| console.log(this.state) | ||
| } else { | ||
| return ( | ||
| <div className="teacherLogin"> | ||
| <h1>Teacher Login</h1> | ||
| <form> | ||
| <label htmlFor="email">Email: </label> | ||
| <input | ||
| type="text" | ||
| name="email" | ||
| value={this.state.email} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="password">Password: </label> | ||
| <input | ||
| type="password" | ||
| name="password" | ||
| value={this.state.password} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <button onClick={this.handleSubmit}>Login</button> | ||
| </form> | ||
|
|
||
| </div> | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default teacherLogin |
| @@ -0,0 +1 @@ | ||
| export { default } from "./teacherSignup"; |
| @@ -0,0 +1,111 @@ | ||
| import React, { Component } from 'react' | ||
| import axios from 'axios' | ||
| import { Redirect } from 'react-router-dom' | ||
|
|
||
| class teacherSignup extends Component { | ||
| constructor() { | ||
| super() | ||
| this.state = { | ||
| fName: '', | ||
| lName: '', | ||
| email: '', | ||
| school: '', | ||
| password: '', | ||
| confirmPassword: '', | ||
| redirectTo: null | ||
| } | ||
| this.handleSubmit = this.handleSubmit.bind(this) | ||
| this.handleChange = this.handleChange.bind(this) | ||
| } | ||
| handleChange(event) { | ||
| this.setState({ | ||
| [event.target.name]: event.target.value | ||
| }) | ||
| } | ||
| handleSubmit(event) { | ||
| event.preventDefault() | ||
| // TODO - validate! | ||
| axios | ||
| .post('/auth/teachersignup', { | ||
| fName: this.state.fName, | ||
| lName: this.state.lName, | ||
| email: this.state.email, | ||
| school: this.state.school, | ||
| phone: this.state.phone, | ||
| password: this.state.password | ||
| }) | ||
| .then(response => { | ||
| console.log(response) | ||
| if (!response.data.errmsg) { | ||
| console.log('youre good') | ||
| this.setState({ | ||
| redirectTo: '/Temp' | ||
| }) | ||
| } else { | ||
| console.log('duplicate') | ||
| } | ||
| }) | ||
| } | ||
| render() { | ||
| if (this.state.redirectTo) { | ||
| return <Redirect to={{ pathname: this.state.redirectTo }} /> | ||
| } | ||
| return ( | ||
| <div className="teacherSignup"> | ||
| <h1>Teacher Signup Form</h1> | ||
| <label htmlFor="fName">Firstname: </label> | ||
| <input | ||
| type="text" | ||
| name="fName" | ||
| value={this.state.fName} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="lName">Lastname: </label> | ||
| <input | ||
| type="text" | ||
| name="lName" | ||
| value={this.state.lName} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="email">Email: </label> | ||
| <input | ||
| type="text" | ||
| name="email" | ||
| value={this.state.email} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="school">School: </label> | ||
| <input | ||
| type="string" | ||
| name="school" | ||
| value={this.state.school} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="phone">Phone: </label> | ||
| <input | ||
| type="string" | ||
| name="phone" | ||
| value={this.state.phone} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="password">Password: </label> | ||
| <input | ||
| type="password" | ||
| name="password" | ||
| value={this.state.password} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <label htmlFor="confirmPassword">Confirm Password: </label> | ||
| <input | ||
| type="password" | ||
| name="confirmPassword" | ||
| value={this.state.confirmPassword} | ||
| onChange={this.handleChange} | ||
| /> | ||
| <button onClick={this.handleSubmit}>Sign up</button> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default teacherSignup |
| @@ -0,0 +1,28 @@ | ||
| /* Mongo Database | ||
| * - this is where we set up our connection to the mongo database | ||
| */ | ||
| const mongoose = require('mongoose') | ||
| mongoose.Promise = global.Promise | ||
| let MONGO_URL | ||
| // const MONGO_LOCAL_URL = 'mongodb://localhost/carpoolGuardian' | ||
|
|
||
| // if (process.env.MONGODB_URI) { | ||
| // mongoose.connect(process.env.MONGODB_URI) | ||
| // MONGO_URL = process.env.MONGODB_URI | ||
| // } else { | ||
| // mongoose.connect(MONGO_LOCAL_URL) // local mongo url | ||
| // MONGO_URL = MONGO_LOCAL_URL | ||
| // } | ||
|
|
||
| // // should mongoose.connection be put in the call back of mongoose.connect??? | ||
| // const db = mongoose.connection | ||
| // db.on('error', err => { | ||
| // console.log(`There was an error connecting to the database: ${err}`) | ||
| // }) | ||
| // db.once('open', () => { | ||
| // console.log( | ||
| // `You have successfully connected to your mongo database: ${MONGO_URL}` | ||
| // ) | ||
| // }) | ||
|
|
||
| // module.exports = db |
| @@ -0,0 +1,44 @@ | ||
| // const mongoose = require('mongoose') | ||
| // const Schema = mongoose.Schema | ||
| // const bcrypt = require('bcryptjs') | ||
| // mongoose.promise = Promise | ||
|
|
||
| // // Define userSchema | ||
| // const teacherSchema = new Schema({ | ||
| // firstName: { type: String, unique: false }, | ||
| // lastName: { type: String, unique: false }, | ||
| // email: {type: String, | ||
| // unique: true, | ||
| // match: [/.+@.+\..+/, "Please enter a valid e-mail address"] | ||
| // }, | ||
| // school: {type: String, unique: false, required: false }, | ||
| // password: { type: String, unique: false, required: false }, | ||
| // photos: [] | ||
|
|
||
| // }) | ||
|
|
||
| // // Define schema methods | ||
| // teacherSchema.methods = { | ||
| // checkPassword: function(inputPassword) { | ||
| // return bcrypt.compareSync(inputPassword, this.password) | ||
| // }, | ||
| // hashPassword: plainTextPassword => { | ||
| // return bcrypt.hashSync(plainTextPassword, 10) | ||
| // } | ||
| // } | ||
|
|
||
| // // Define hooks for pre-saving | ||
| // teacherSchema.pre('save', function(next) { | ||
| // if (!this.password) { | ||
| // console.log('=======NO PASSWORD PROVIDED=======') | ||
| // next() | ||
| // } else { | ||
| // this.password = this.hashPassword(this.password) | ||
| // next() | ||
| // } | ||
|
|
||
| // }) | ||
|
|
||
| // // Create reference to User & export | ||
| // const Teacher = mongoose.model('Teacher', teacherSchema) | ||
| // module.exports = Teacher |
| @@ -0,0 +1,45 @@ | ||
| const mongoose = require('mongoose') | ||
| const Schema = mongoose.Schema | ||
| const bcrypt = require('bcryptjs') | ||
| mongoose.promise = Promise | ||
|
|
||
| // Define userSchema | ||
| const userSchema = new Schema({ | ||
| firstName: { type: String, unique: false }, | ||
| lastName: { type: String, unique: false }, | ||
| email: {type: String, | ||
| unique: true, | ||
| match: [/.+@.+\..+/, "Please enter a valid e-mail address"] | ||
| }, | ||
| password: { type: String, unique: false, required: false }, | ||
|
|
||
| photos: [] | ||
|
|
||
| }) | ||
|
|
||
| // Define schema methods | ||
| userSchema.methods = { | ||
| checkPassword: function(inputPassword) { | ||
| return bcrypt.compareSync(inputPassword, this.password) | ||
| }, | ||
| hashPassword: plainTextPassword => { | ||
| return bcrypt.hashSync(plainTextPassword, 10) | ||
| } | ||
| } | ||
|
|
||
| // Define hooks for pre-saving | ||
| userSchema.pre('save', function(next) { | ||
| if (!this.password) { | ||
| console.log('=======NO PASSWORD PROVIDED=======') | ||
| next() | ||
| } else { | ||
| this.password = this.hashPassword(this.password) | ||
| next() | ||
| } | ||
| // this.password = this.hashPassword(this.password) | ||
| // next() | ||
| }) | ||
|
|
||
| // Create reference to User & export | ||
| const User = mongoose.model('User', userSchema) | ||
| module.exports = User |
| @@ -0,0 +1,48 @@ | ||
| const passport = require('passport') | ||
| const LocalStrategy = require('./localStrategy') | ||
| const teacherStrategy = require('./teacherstrategy') | ||
| // const GoogleStratgey = require('./googleStrategy') | ||
| const Guardian = require('../models/guardian') | ||
| const Teacher = require('../models/teacher'); | ||
|
|
||
| passport.serializeUser((user, done) => { | ||
| console.log('=== serialize ... called ===') | ||
| console.log(user) // the whole raw user object! | ||
| console.log('---------') | ||
| done(null, { _id: user._id }) | ||
| }) | ||
|
|
||
| passport.deserializeUser((id, done) => { | ||
| (Guardian.findOne( | ||
| { _id: id } === null)) ? | ||
| Teacher.findOne( | ||
| { _id: id }, | ||
| 'fName lName school email', | ||
| (err, user) => { | ||
| console.log('======= DESERIALIZE USER CALLED ======') | ||
| console.log(user) | ||
| console.log('--------------') | ||
| done(null, user) | ||
| } | ||
| ) : | ||
| Guardian.findOne( | ||
| { _id: id }, | ||
| 'fName lName phone email', | ||
| (err, user) => { | ||
| console.log('======= DESERIALIZE USER CALLED ======') | ||
| console.log(user) | ||
| console.log('--------------') | ||
| done(null, user) | ||
| } | ||
| ) | ||
|
|
||
| }) | ||
|
|
||
|
|
||
|
|
||
| // ==== Register Strategies ==== | ||
| passport.use('local', LocalStrategy) | ||
| passport.use('local.teacher', teacherStrategy) | ||
| // passport.use(GoogleStratgey) | ||
|
|
||
| module.exports = passport |
| @@ -0,0 +1,27 @@ | ||
| const Guardian = require('../models/guardian') | ||
|
|
||
| const LocalStrategy = require('passport-local').Strategy | ||
|
|
||
| const strategy = new LocalStrategy( | ||
| { | ||
| usernameField: 'email' // not necessary, DEFAULT | ||
| }, | ||
| function(email, password, done) { | ||
| Guardian.findOne({ 'email': email }, (err, userMatch) => { | ||
| if (err) { | ||
| return done(err) | ||
| } | ||
| if (!userMatch) { | ||
| return done(null, false, { message: 'Incorrect username' }) | ||
| } | ||
| if (!userMatch.checkPassword(password)) { | ||
| return done(null, false, { message: 'Incorrect password' }) | ||
| } | ||
| return done(null, userMatch) | ||
| }) | ||
| } | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| module.exports = strategy |
| @@ -0,0 +1,25 @@ | ||
| const Teacher = require('../models/teacher'); | ||
|
|
||
| const LocalStrategy = require('passport-local').Strategy | ||
|
|
||
| const teacherstrategy = new LocalStrategy( | ||
| { | ||
| usernameField: 'email' // not necessary, DEFAULT | ||
| }, | ||
| function(email, password, done) { | ||
| Teacher.findOne({ 'email': email }, (err, teacherMatch) => { | ||
| if (err) { | ||
| return done(err) | ||
| } | ||
| if (!teacherMatch) { | ||
| return done(null, false, { message: 'Incorrect username' }) | ||
| } | ||
| if (!teacherMatch.checkPassword(password)) { | ||
| return done(null, false, { message: 'Incorrect password' }) | ||
| } | ||
| return done(null, teacherMatch) | ||
| }) | ||
| } | ||
| ) | ||
|
|
||
| module.exports = teacherstrategy |
| @@ -0,0 +1,124 @@ | ||
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const Guardian = require('../../models/guardian'); | ||
| const Teacher = require('../../models/teacher'); | ||
| const passport = require('../../passport'); | ||
|
|
||
|
|
||
| // this route is just used to get the user basic info | ||
| router.get('/user', (req, res, next) => { | ||
| console.log('===== user!!======') | ||
| console.log(req.user) | ||
| if (req.user) { | ||
| return res.json({ user: req.user }) | ||
| } else { | ||
| return res.json({ user: null }) | ||
| } | ||
| }) | ||
|
|
||
| router.post( | ||
| '/login', | ||
| function(req, res, next) { | ||
| console.log(req.body) | ||
| // console.log('=====testing===========') | ||
| next() | ||
| }, | ||
| passport.authenticate('local'), | ||
| (req, res) => { | ||
| // console.log('POST to /login') | ||
| const user = JSON.parse(JSON.stringify(req.user)) // hack | ||
| const cleanUser = Object.assign({}, user) | ||
| if (cleanUser) { | ||
| console.log(`Deleting ${cleanUser.password}`) | ||
| delete cleanUser.password | ||
| } | ||
| res.json({ user: cleanUser }) | ||
| // res.redirect('/Temp'); | ||
|
|
||
| } | ||
| ) | ||
|
|
||
| router.post( | ||
| '/teacherlogin', | ||
| function(req, res, next) { | ||
| console.log(req.body) | ||
| // console.log('=====testing===========') | ||
| next() | ||
| }, | ||
| passport.authenticate('local.teacher'), | ||
| (req, res) => { | ||
| // console.log('POST to /login') | ||
| const user = JSON.parse(JSON.stringify(req.user)) // hack | ||
| const cleanUser = Object.assign({}, user) | ||
| if (cleanUser) { | ||
| console.log(`Deleting ${cleanUser.password}`) | ||
| delete cleanUser.password | ||
| } | ||
| res.json({ user: cleanUser }) | ||
| // res.redirect('/Temp'); | ||
|
|
||
| } | ||
| ) | ||
|
|
||
| router.post('/logout', (req, res) => { | ||
| if (req.user) { | ||
| req.session.destroy() | ||
| res.clearCookie('connect.sid') // clean up! | ||
| return res.json({ msg: 'logging you out' }) | ||
| } else { | ||
| return res.json({ msg: 'no user to log out!' }) | ||
| } | ||
| }) | ||
|
|
||
| router.post('/signup', (req, res) => { | ||
| const { fName, lName, email, phone, password } = req.body | ||
| console.log(req.body); | ||
| // ADD VALIDATION | ||
| Guardian.findOne({ 'email': email }, (err, userMatch) => { | ||
| if (userMatch) { | ||
| res.json({ | ||
| error: `Sorry, already a user with the email: ${email}` | ||
| }) | ||
| } | ||
| const newGuardian = new Guardian({ | ||
| 'fName': fName, | ||
| 'lName': lName, | ||
| 'email': email, | ||
| 'phone': phone, | ||
| 'password': password | ||
| }) | ||
| newGuardian.save((err, savedUser) => { | ||
| console.log(savedUser); | ||
| if (err) throw err; | ||
| res.json(savedUser) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| router.post('/teachersignup', (req, res) => { | ||
| const { fName, lName, email, school, phone, password } = req.body | ||
| console.log(req.body); | ||
| // ADD VALIDATION | ||
| Teacher.findOne({ 'email': email }, (err, teacherMatch) => { | ||
| if (teacherMatch) { | ||
| return res.json({ | ||
| error: `Sorry, already a teacher with the email: ${email}` | ||
| }) | ||
| } | ||
| const newTeacher = new Teacher({ | ||
| 'fName': fName, | ||
| 'lName': lName, | ||
| 'email': email, | ||
| 'school': school, | ||
| 'phone': phone, | ||
| 'password': password | ||
| }) | ||
| newTeacher.save((err, savedTeacher) => { | ||
| console.log(savedTeacher); | ||
| if (err) throw err; | ||
| res.json(savedTeacher) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| module.exports = router |
| @@ -1,13 +1,16 @@ | ||
| // const path = require("path"); | ||
| const router = require("express").Router(); | ||
| const apiRoutes = require("./api"); | ||
| const authRoutes = require("./auth"); | ||
|
|
||
| // API Routes | ||
| router.use("/api", apiRoutes); | ||
| // auth Routes | ||
| router.use("/auth", authRoutes); | ||
|
|
||
| // // If no API routes are hit, send the React app | ||
| // router.use(function(req, res) { | ||
| // res.sendFile(path.join(__dirname, "../client/public/index.html")); | ||
| // }); | ||
|
|
||
| module.exports = router; |