Skip to content

Commit cdd3024

Browse files
committed
Merge branch 'garrett/statecontexttest', remote-tracking branch 'origin' into mike/statecontexttest
3 parents 86266a3 + 0ef2842 + 02efa7c commit cdd3024

File tree

11 files changed

+177
-32
lines changed

11 files changed

+177
-32
lines changed

app/src/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const App = (): JSX.Element => {
6868
}
6969
});
7070
}
71-
}, [state.isLoggedIn]);
71+
}, []);
7272
useEffect(() => {
7373
// provide config properties to legacy projects so new edits can be auto saved
7474
// if (state.config === undefined) {

app/src/components/login/SignIn.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
172172
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
173173
) => {
174174
e.preventDefault();
175-
window.api.github();
176-
props.history.push('/');
175+
// window.api.github();
176+
window.location.assign('http://localhost:5656/auth/github');
177177
}
178178
const responseFacebook = response => {
179179
if (response.accessToken) {
@@ -284,6 +284,18 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
284284
>
285285
Sign In With Github
286286
</Button>
287+
<Button
288+
fullWidth
289+
variant="contained"
290+
color="primary"
291+
id="SignInWithGoogle"
292+
onClick={(e)=>{
293+
e.preventDefault();
294+
window.location.assign('http://localhost:5656/auth/google');
295+
}}
296+
>
297+
Sign in With Google
298+
</Button>
287299
<Button
288300
fullWidth
289301
variant="contained"

app/src/components/right/LoginButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export default function LoginButton() {
3232
window.location.href = '/index-prod.html'
3333
} else {
3434
window.location.href = 'http://localhost:8080/#/login';
35-
dispatch(toggleLoggedIn())
35+
if(state.isLoggedIn){
36+
dispatch(toggleLoggedIn())
37+
}
38+
3639
}
3740
}
3841
if (state.isLoggedIn) {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
"electron-window-manager": "^1.0.6",
157157
"esbuild-wasm": "^0.14.51",
158158
"eslint-plugin-react-hooks": "^4.6.0",
159+
"express-session": "^1.17.3",
159160
"fs": "^0.0.1-security",
160161
"graphql": "^16.6.0",
161162
"identity-obj-proxy": "^3.0.0",
@@ -166,6 +167,7 @@
166167
"node-fetch": "^2.6.1",
167168
"passport": "^0.6.0",
168169
"passport-github2": "^0.1.12",
170+
"passport-google-oauth20": "^2.0.0",
169171
"prettier": "^2.7.1",
170172
"prop-types": "^15.8.1",
171173
"radium": "^0.26.2",

server/controllers/sessionController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ sessionController.gitHubSendToken = (req, res, next) => {
140140
// creates a session when logging in with github
141141
sessionController.githubSession = (req, res, next) => {
142142
// req.user is passed in from passport js -> serializeuser/deserializeuser
143-
const cookieId = req.user._id;
143+
const cookieId = req.user.id;
144144
Sessions.findOne({ cookieId }, (err, session) => {
145145
if (err) {
146146
return next({

server/models/Oauth-model.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const mongoose = require('mongoose');
2+
const Schema = mongoose.Schema
3+
4+
const userSchema = new Schema({
5+
username: { type: String },
6+
githubId: { type: String, unique: true },
7+
googleId: { type: String, unique: true }
8+
})
9+
10+
const User = mongoose.model('user', userSchema)
11+
12+
module.exports = User;

server/models/reactypeModels.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
const mongoose = require('mongoose');
1212
const bcrypt = require('bcryptjs');
1313
require('dotenv').config();
14-
const mongoURI = process.env.MONGO_DB_DEV;
14+
const mongoURI = process.env.MONGO_DB
1515
const URI =
16-
process.env.NODE_ENV === 'production' ? mongoURI : process.env.MONGO_DB_DEV;
16+
process.env.NODE_ENV === 'production' ? mongoURI : process.env.MONGO_DB;
1717

1818
const SALT_WORK_FACTOR = 10;
1919
// connect to mongo db

server/routers/auth.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const router = require('express').Router();
2+
const passport = require('passport');
3+
4+
router.get(
5+
'/github',
6+
passport.authenticate('github', {
7+
scope: ['profile']
8+
})
9+
);
10+
11+
router.get(
12+
'/github/callback',
13+
passport.authenticate('github'),
14+
(req, res) => {
15+
console.log('this authenticate function is being run')
16+
console.log(req.user.id)
17+
res.cookie('ssid', req.user.id)
18+
return res.redirect('http://localhost:8080')
19+
})
20+
21+
router.get('/google', passport.authenticate('google', {
22+
scope: ['profile']
23+
}))
24+
25+
router.get('/google/callback', passport.authenticate('google'), (req, res) => {
26+
console.log('google authenicate function being run')
27+
res.cookie('ssid', req.user.id)
28+
return res.redirect('http://localhost:8080')
29+
})
30+
31+
module.exports = router;

server/routers/passport-setup.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const passport = require('passport');
2+
const GitHubStrategy = require('passport-github2').Strategy
3+
const user = require('../models/Oauth-model');
4+
const GoogleStrategy = require('passport-google-oauth20')
5+
6+
passport.serializeUser((user, done) => {
7+
done(null, user.id)
8+
})
9+
10+
passport.deserializeUser((id, done) => {
11+
user.findById(id)
12+
.then(user=>{
13+
done(null, user)
14+
})
15+
})
16+
17+
passport.use(
18+
new GitHubStrategy(
19+
{
20+
clientID: process.env.GITHUB_CLIENT,
21+
clientSecret: process.env.GITHUB_SECRET,
22+
callbackURL: `http://localhost:5656/auth/github/callback`
23+
},
24+
function(accessToken, refreshToken, profile, done) {
25+
console.log(profile);
26+
user.findOne({
27+
githubId: profile.id
28+
})
29+
.then(currentUser => {
30+
if (currentUser) {
31+
console.log('user is: ', currentUser)
32+
return done(null, currentUser)
33+
} else {
34+
user.create({
35+
username: profile.displayName+'(Github)',
36+
githubId: profile.id
37+
})
38+
.then(data=>{
39+
console.log('user added successfully: ', data);
40+
return done(null, data)
41+
})
42+
.catch(data=>console.log('issue with adding user to database', data))
43+
}
44+
})
45+
}
46+
)
47+
);
48+
49+
passport.use(
50+
new GoogleStrategy(
51+
{
52+
clientID: process.env.GOOGLE_CLIENT,
53+
clientSecret: process.env.GOOGLE_SECRET,
54+
callbackURL: `http://localhost:5656/auth/google/callback`
55+
},
56+
function(accessToken, refreshToken, profile, done) {
57+
console.log(profile);
58+
user.findOne({
59+
googleId: profile.id
60+
})
61+
.then(currentUser => {
62+
if (currentUser) {
63+
console.log('user is: ', currentUser)
64+
return done(null, currentUser)
65+
} else {
66+
user.create({
67+
username: profile.displayName+'(Google)',
68+
googleId: profile.id
69+
})
70+
.then(data=>{
71+
console.log('user added successfully: ', data);
72+
return done(null, data)
73+
})
74+
.catch(data=>console.log('issue with adding user to database', data))
75+
}
76+
})
77+
}
78+
)
79+
);

server/server.mjs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,29 @@ app.use(
6565

6666
// NOTE from v13.0 team: GitHub OAuth works fine in Electron production app and the backend for Electron production app is deployed on Heroku at https://reactype-caret.herokuapp.com/ (get credentials from instructor )
6767

68-
// passport.use(
69-
// new GitHubStrategy(
70-
// {
71-
// clientID: process.env.GITHUB_ID,
72-
// clientSecret: process.env.GITHUB_SECRET,
73-
// callbackURL: isDev
74-
// ? `http://localhost:${DEV_PORT}/github/callback`
75-
// : `https://reactype-caret.herokuapp.com/github/callback`
76-
// },
77-
// function(accessToken, refreshToken, profile, done) {
78-
// console.log(profile);
79-
// }
80-
// )
81-
// );
68+
// V.15 Team: Github Oauth and Google Oauth works! (starts here)
69+
// const passport = require('passport');
70+
import passport from 'passport';
71+
// const passportSetup = require('./routers/passport-setup.js')
72+
import passportSetup from './routers/passport-setup.js'
73+
// const session = require('express-session');
74+
import session from 'express-session'
75+
// const authRoutes = require('./routers/auth.js')
76+
import authRoutes from './routers/auth.js'
77+
78+
app.use(session({
79+
secret: process.env.SESSION_SECRET,
80+
resave: false,
81+
saveUninitialized: true,
82+
cookie: { maxAge: 24*60*60*1000 }
83+
}))
84+
85+
app.use(passport.initialize());
86+
app.use(passport.session());
87+
88+
// go to other files
89+
app.use('/auth', authRoutes)
8290

83-
// initializes passport and passport sessions
84-
// app.use(passport.initialize());
85-
// app.use(passport.session());
86-
87-
// app.get(
88-
// '/auth/github',
89-
// passport.authenticate('github', { session: false }),
90-
// (req, res) => {
91-
// res.send('github');
92-
// }
93-
// );
9491

9592
// for Oauth which is currently not working
9693
// app.get(
@@ -230,6 +227,10 @@ if (process.env.NODE_ENV == 'production'){
230227
});
231228
}
232229

230+
app.get('/test', (req, res) => {
231+
res.send('test request is working');
232+
})
233+
233234
app.get('/', function(req, res) {
234235
res.send('Houston, Caret is in orbit!');
235236
});

webpack.development.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = merge(base, {
1212
mode: 'development',
1313
devtool: 'inline-source-map',
1414
devServer: {
15+
headers: { 'Access-Control-Allow-Origin': '*' },
16+
historyApiFallback: true,
1517
host: 'localhost',
1618
port: '8080',
1719
hot: true, // Hot-reload this server if changes are detected
@@ -27,6 +29,9 @@ module.exports = merge(base, {
2729
},
2830
'/user-styles': {
2931
target: `http://localhost:${DEV_PORT}/`
32+
},
33+
'/auth/**': {
34+
target: `http://localhost:5656/`
3035
}
3136
}
3237
},

0 commit comments

Comments
 (0)