Spaces:
Running
Running
now i want to add proper authentication where user can login only with their google account and Users should be able to sign up, log in, and log out securely Once logged in, users should automatically be taken to the chat interface. Each user should have their own chat history β so when they log in again later, they can see their previous conversations in the sidebar
590d81d
verified
| ```javascript | |
| const { OAuth2Client } = require('google-auth-library'); | |
| const express = require('express'); | |
| const session = require('express-session'); | |
| const dotenv = require('dotenv'); | |
| const path = require('path'); | |
| dotenv.config(); | |
| const app = express(); | |
| const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID); | |
| // Middleware | |
| app.use(express.json()); | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| app.use(session({ | |
| secret: process.env.SESSION_SECRET, | |
| resave: false, | |
| saveUninitialized: false, | |
| cookie: { secure: process.env.NODE_ENV === 'production', maxAge: 24 * 60 * 60 * 1000 } | |
| })); | |
| // Google OAuth routes | |
| app.get('/auth/google', (req, res) => { | |
| const url = client.generateAuthUrl({ | |
| access_type: 'online', | |
| scope: ['profile', 'email'], | |
| redirect_uri: process.env.GOOGLE_REDIRECT_URI | |
| }); | |
| res.redirect(url); | |
| }); | |
| app.get('/auth/google/callback', async (req, res) => { | |
| const { code } = req.query; | |
| try { | |
| const { tokens } = await client.getToken({ | |
| code, | |
| redirect_uri: process.env.GOOGLE_REDIRECT_URI | |
| }); | |
| const ticket = await client.verifyIdToken({ | |
| idToken: tokens.id_token, | |
| audience: process.env.GOOGLE_CLIENT_ID | |
| }); | |
| const payload = ticket.getPayload(); | |
| req.session.user = { | |
| id: payload.sub, | |
| email: payload.email, | |
| name: payload.name, | |
| picture: payload.picture | |
| }; | |
| res.redirect('/'); | |
| } catch (error) { | |
| console.error('Auth error:', error); | |
| res.redirect('/?auth_error=1'); | |
| } | |
| }); | |
| app.get('/auth/logout', (req, res) => { | |
| req.session.destroy(); | |
| res.redirect('/'); | |
| }); | |
| app.get('/auth/status', (req, res) => { | |
| res.json({ isAuthenticated: !!req.session.user, user: req.session.user }); | |
| }); | |
| // Chat history routes | |
| app.get('/api/chats', async (req, res) => { | |
| if (!req.session.user) return res.sendStatus(401); | |
| // In a real app, you'd fetch from a database | |
| const chats = JSON.parse(localStorage.getItem(`chats_${req.session.user.id}`) || '[]'); | |
| res.json(chats); | |
| }); | |
| app.post('/api/chats', async (req, res) => { | |
| if (!req.session.user) return res.sendStatus(401); | |
| // In a real app, you'd save to a database | |
| const chats = JSON.parse(localStorage.getItem(`chats_${req.session.user.id}`) || '[]'); | |
| chats.push(req.body); | |
| localStorage.setItem(`chats_${req.session.user.id}`, JSON.stringify(chats)); | |
| res.sendStatus(201); | |
| }); | |
| const PORT = process.env.PORT || 3000; | |
| app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); | |
| ``` |