Skip to content

Commit

Permalink
created google login
Browse files Browse the repository at this point in the history
  • Loading branch information
OmSingh5092 committed Oct 5, 2020
1 parent ad8f472 commit bd2193c
Show file tree
Hide file tree
Showing 23 changed files with 284 additions and 35 deletions.
6 changes: 6 additions & 0 deletions Backend/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ const app = express();
const config = require('./config');
const database = require('./database/database');
const session = require('express-session');
const cors = require('cors');

require('./passport');

//Applying middlewares
app.use(express.json());
app.use(session(config.session));
app.use(cors());

//Importing rotues
const signInRoute = require('./routes/signinRouter');
const profileRoute =require('./routes/profileRouter');

//Applying routes
app.use('/api/signin',signInRoute);
app.use('/api/profile',profileRoute);

app.listen(config.app.local.port, ()=>{
console.log("\n\n App listening... \n\n");
Expand Down
4 changes: 2 additions & 2 deletions Backend/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const config = {
}
},
gcp:{
clientId: "541374752269-86ein6vehn2elteuea39arj5nnaok92o.apps.googleusercontent.com",
clientSecret:"iKd6K67CdlmgVEvKHSclJ9Mf"
clientId: "41880489918-ff5sebqstbkdjru2po7gmgsepqhnuio7.apps.googleusercontent.com",
clientSecret:"-1JWDbVE3ytYo5_qpXwPYHje"
},
}

Expand Down
22 changes: 22 additions & 0 deletions Backend/controllers/profileCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Interviewer = require('../database/schema/interviewer');

const getProfile = async (req,res)=>{
const id = req.user.id;

try{
const profile = await Interviewer.findOne({id:id});

return res.status(200).json({
success:true,
profile:profile,
})
}catch(err){
console.log("Error",err);
return res.status(500).json({
success:false,
msg:"Internal Server Error",
})
}
}

module.exports = {getProfile};
10 changes: 8 additions & 2 deletions Backend/controllers/signInCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const jwt = require('jsonwebtoken');
const config = require('../config');

const Interviewer = require('../database/schema/interviewer');
const createUser = async (req,res)=>{
const googleSignIn= async (req,res)=>{
const idToken = req.body.idToken;
const client = new OAuth2Client(config.gcp.clientId);

Expand Down Expand Up @@ -66,4 +66,10 @@ const createUser = async (req,res)=>{
}
}

module.exports = {createUser};
const emailSignIn = (req,res)=>{

}



module.exports = {googleSignIn,emailSignIn};
5 changes: 5 additions & 0 deletions Backend/middlewares/verifyMW.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var passport = require('passport');

const user = passport.authenticate('jwt', { session: false });

module.exports = {user}
37 changes: 37 additions & 0 deletions Backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
"license": "ISC",
"dependencies": {
"@material-ui/core": "^4.11.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-session": "^1.17.1",
"google-auth-library": "^6.1.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.7",
"nodemon": "^2.0.4",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"pg": "^8.3.3",
"sequelize": "^6.3.5"
}
Expand Down
11 changes: 11 additions & 0 deletions Backend/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var passport = require('passport');
var JWTStrategy = require('passport-jwt').Strategy

var config = require('./config')

passport.use(new JWTStrategy({
jwtFromRequest: req => req.headers.token,
secretOrKey: config.jwt.TOKEN_SECRET
},(jwtPayload, done) => {
return done(null, jwtPayload)
}))
9 changes: 9 additions & 0 deletions Backend/routes/profileRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();

const profileCtrl = require('../controllers/profileCtrl')
const verifyUser = require('../middlewares/verifyMW').user;

router.get('/interviewer/own',verifyUser,profileCtrl.getProfile);

module.exports = router;
3 changes: 2 additions & 1 deletion Backend/routes/signinRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router();

const signInCtrl = require('../controllers/signInCtrl');

router.post('/interviewer',signInCtrl.createUser);
router.post('/interviewer/google',signInCtrl.googleSignIn);
router.post('/interviewer/email',signInCtrl.emailSignIn);

module.exports = router;
9 changes: 9 additions & 0 deletions Frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"firebase": "^7.22.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-google-login": "^5.1.21",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3"
},
Expand Down
11 changes: 3 additions & 8 deletions Frontend/src/app/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import {HashRouter,Switch, Route,withRouter} from 'react-router-dom'
import {useEffect} from 'react'
import {firebaseConfig} from './config'
import firebase from 'firebase'

import LoginScreen from './components/screens/LoginScreen'

Expand All @@ -24,14 +25,8 @@ function Main(props){
googleSigninScript.src = "https://apis.google.com/js/platform.js";
googleSigninScript.onload = ()=>{setScriptLoading(scriptLoading+1)};

const firebaseScript = document.createElement('script');
firebaseScript.src = "https://www.gstatic.com/firebasejs/7.22.0/firebase-app.js";
firebaseScript.onload = ()=>{
window.firebase.initializeApp(firebaseConfig);
setScriptLoading(scriptLoading+1);
};

document.body.append(firebaseScript);
firebase.initializeApp(firebaseConfig);

document.body.append(googleSigninScript);
},[1])

Expand Down
31 changes: 22 additions & 9 deletions Frontend/src/app/components/atoms/GoogleLoginButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ import React from 'react'
import {ButtonBase,Typography} from '@material-ui/core'

import GoogleLogo from '../../res/icons/google.png'
import {GoogleLogin} from 'react-google-login'

import {googleConfig} from '../../config'


function GoogleLoginButton(props){
const onClick = props.onClick;
const onSuccess = props.onSuccess;
const onFailure = props.onFailure;

return(
<div>
<ButtonBase onClick={onClick} >
<div style={{display:"flex", flexDirection:"row", justifyContent:"center", margin:20, }}>
<img src = {GoogleLogo} style={{height:20, width:20}}/>
<Typography style={{marginLeft:10}}>
SignIn with Google
</Typography>
</div>
</ButtonBase>
<GoogleLogin
render={(renderProps)=>(
<ButtonBase onClick={renderProps.onClick}>
<div style={{display:"flex", flexDirection:"row", justifyContent:"center", margin:20, }}>
<img src = {GoogleLogo} style={{height:20, width:20}}/>
<Typography style={{marginLeft:10}}>
SignIn with Google
</Typography>
</div>
</ButtonBase>
)}
onSuccess={onSuccess}
onFailure={onFailure}
clientId={googleConfig.clientId}
cookiePolicy={'single_host_origin'}
/>

</div>

)
Expand Down
57 changes: 48 additions & 9 deletions Frontend/src/app/components/screens/LoginScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,56 @@ import React from 'react'

import {Button,Dialog, DialogContent, DialogTitle,DialogActions, ButtonBase} from '@material-ui/core'
import './style.css';
import {withRouter} from 'react-router-dom'

import IntervierLogin from '../molecules/InterViewerLogin';
import JoinRoomForm from '../molecules/JoinRoomForm';
import GoogleLoginButton from '../atoms/GoogleLoginButton';

function InterviewerDialog(props){
import {googleSignIn} from '../../utils/api/controllers/signInCtrl'
import {getOwnProfile} from '../../utils/api/controllers/profileCtrl'

import {UserData} from '../../utils/localStorage';

const InterviewerDialog = withRouter((props)=>{
const isOpen = props.isOpen;
const onClose = props.onClose;

const googleLoginSuccess = (response)=>{
var profile = response.profileObj
console.log("Login");
googleSignIn(response).then((res)=>(res.json()))
.then((data)=>{
console.log("login successfull");
//Saving data in
//Getting Success.
if(data.success){
UserData.setToken(data.jwt);
//setting email
UserData.setEmail(profile.email);
if(data.newUser){
props.history.push('/register');
}else{
console.log(UserData.getToken());
getOwnProfile().then((res)=>(res.json()))
.then((res)=>{
if(res.success){
UserData.setProfileData(res.profile);
props.history.push('/interviewer');
}else{
console.log(res.msg);
}

})

}
}else{
console.log(data.msg);
}
}).catch((err)=>{
console.log("Error",err);
})
}
return(
<Dialog open={isOpen} onClose = {onClose}>
<DialogTitle>Sign In As An Interviewer</DialogTitle>
Expand All @@ -21,7 +63,7 @@ function InterviewerDialog(props){
</div>
</DialogContent>
<div style={{margin:"auto"}}>
<GoogleLoginButton onClick = {()=>{}}/>
<GoogleLoginButton onSuccess= {googleLoginSuccess} onFailure = {(response)=>{console.log("Error",response)}}/>
</div>
<div style={{margin:"auto"}} >
<ButtonBase onClick={()=>{}} >
Expand All @@ -30,9 +72,6 @@ function InterviewerDialog(props){
</div>
</ButtonBase>
</div>



<DialogActions>
<Button onClick={onClose}>
Cancle
Expand All @@ -41,9 +80,9 @@ function InterviewerDialog(props){
</Dialog>

)
}
});

function CandidateDialog(props){
const CandidateDialog = withRouter((props)=>{
const isOpen = props.isOpen;
const onClose = props.onClose;
return(
Expand All @@ -61,7 +100,7 @@ function CandidateDialog(props){
</DialogActions>
</Dialog>
)
}
})

function LoginScreen(props){
const [interviewerDialog,setInterviewDialog] = React.useState(false);
Expand Down Expand Up @@ -109,4 +148,4 @@ function LoginScreen(props){
</div>)
}

export default LoginScreen;
export default withRouter(LoginScreen);
Loading

0 comments on commit bd2193c

Please sign in to comment.