Skip to content

Commit

Permalink
Merge pull request #44 from rogershi-dev/feature/registration-auth
Browse files Browse the repository at this point in the history
Added session to store temporary auth data
  • Loading branch information
rogershi-dev authored Jul 10, 2024
2 parents 7fb2e34 + fd2fba0 commit f93cb5e
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 8 deletions.
37 changes: 35 additions & 2 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
require('dotenv').config(); // Load environmental variables from .env file
var createError = require('http-errors');
var express = require('express');
const path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');

// Routes
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var authRouter = require('./routes/auth');

// Session storage
const session = require('express-session');
const MySQLStore = require('express-mysql-session')(session);

var app = express();
const port = process.env.PORT || 5000;

// MySQL session store configuration
const sessionStore = new MySQLStore({
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});

// Use session middleware
app.use(session({
key: 'temp_data',
secret: process.env.SESSION_SECRET,
store: sessionStore,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: process.env.NODE_ENV === 'production'
}
}));

// Body parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // Set extended to true to handle complex data structures

// view engine setup changed from jade to pug
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

Expand Down
115 changes: 112 additions & 3 deletions server/package-lock.json

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

2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"express": "~4.16.1",
"express-mysql-session": "^3.0.3",
"express-session": "^1.18.0",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"masto": "^6.8.0",
Expand Down
13 changes: 10 additions & 3 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const axios = require('axios');
const router = express.Router();
require('dotenv').config();
const pool = require('../db.js'); // Import the MySQL connection pool


const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID;
Expand Down Expand Up @@ -32,20 +33,24 @@ router.get('/github/callback', async (req, res) => {
Accept: 'application/json'
},
});

// Extract the access token
const accessToken = tokenResponse.data.access_token;

// Fetch user information from GitHub
// Fetch user information from GitHub using the fetched access token
const githubResponse = await axios.get('https://api.github.com/user', {
headers: {
Authorization: `token ${accessToken}`
}
});

// Extract the user's GitHub username
const githubUsername = githubResponse.data.login;
console.log(githubResponse.data);
console.log(githubUsername);

// Store Github username and access token in session for later use with LinkedIn OAuth
req.session.githubUsername = githubUsername;
req.session.githubAccessToken = accessToken;

// Redirect to the registration page with the GitHub username
res.redirect(`/users/register?githubUsername=${githubUsername}`);
} catch (error) {
Expand All @@ -56,4 +61,6 @@ router.get('/github/callback', async (req, res) => {
});




module.exports = router;

0 comments on commit f93cb5e

Please sign in to comment.