Spaces:
Running
Running
Removed files from promoting 6f90744
Browse filesRemoved files that don't exist in commit 6f907443fea144f861fa6a07196de854cb670270:
- auth.js
auth.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
```javascript
|
| 2 |
-
const { OAuth2Client } = require('google-auth-library');
|
| 3 |
-
const express = require('express');
|
| 4 |
-
const session = require('express-session');
|
| 5 |
-
const dotenv = require('dotenv');
|
| 6 |
-
const path = require('path');
|
| 7 |
-
|
| 8 |
-
dotenv.config();
|
| 9 |
-
|
| 10 |
-
const app = express();
|
| 11 |
-
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
|
| 12 |
-
|
| 13 |
-
// Middleware
|
| 14 |
-
app.use(express.json());
|
| 15 |
-
app.use(express.static(path.join(__dirname, 'public')));
|
| 16 |
-
app.use(session({
|
| 17 |
-
secret: process.env.SESSION_SECRET,
|
| 18 |
-
resave: false,
|
| 19 |
-
saveUninitialized: false,
|
| 20 |
-
cookie: { secure: process.env.NODE_ENV === 'production', maxAge: 24 * 60 * 60 * 1000 }
|
| 21 |
-
}));
|
| 22 |
-
|
| 23 |
-
// Google OAuth routes
|
| 24 |
-
app.get('/auth/google', (req, res) => {
|
| 25 |
-
const url = client.generateAuthUrl({
|
| 26 |
-
access_type: 'online',
|
| 27 |
-
scope: ['profile', 'email'],
|
| 28 |
-
redirect_uri: process.env.GOOGLE_REDIRECT_URI
|
| 29 |
-
});
|
| 30 |
-
res.redirect(url);
|
| 31 |
-
});
|
| 32 |
-
|
| 33 |
-
app.get('/auth/google/callback', async (req, res) => {
|
| 34 |
-
const { code } = req.query;
|
| 35 |
-
|
| 36 |
-
try {
|
| 37 |
-
const { tokens } = await client.getToken({
|
| 38 |
-
code,
|
| 39 |
-
redirect_uri: process.env.GOOGLE_REDIRECT_URI
|
| 40 |
-
});
|
| 41 |
-
|
| 42 |
-
const ticket = await client.verifyIdToken({
|
| 43 |
-
idToken: tokens.id_token,
|
| 44 |
-
audience: process.env.GOOGLE_CLIENT_ID
|
| 45 |
-
});
|
| 46 |
-
|
| 47 |
-
const payload = ticket.getPayload();
|
| 48 |
-
req.session.user = {
|
| 49 |
-
id: payload.sub,
|
| 50 |
-
email: payload.email,
|
| 51 |
-
name: payload.name,
|
| 52 |
-
picture: payload.picture
|
| 53 |
-
};
|
| 54 |
-
|
| 55 |
-
res.redirect('/');
|
| 56 |
-
} catch (error) {
|
| 57 |
-
console.error('Auth error:', error);
|
| 58 |
-
res.redirect('/?auth_error=1');
|
| 59 |
-
}
|
| 60 |
-
});
|
| 61 |
-
|
| 62 |
-
app.get('/auth/logout', (req, res) => {
|
| 63 |
-
req.session.destroy();
|
| 64 |
-
res.redirect('/');
|
| 65 |
-
});
|
| 66 |
-
|
| 67 |
-
app.get('/auth/status', (req, res) => {
|
| 68 |
-
res.json({ isAuthenticated: !!req.session.user, user: req.session.user });
|
| 69 |
-
});
|
| 70 |
-
|
| 71 |
-
// Chat history routes
|
| 72 |
-
app.get('/api/chats', async (req, res) => {
|
| 73 |
-
if (!req.session.user) return res.sendStatus(401);
|
| 74 |
-
|
| 75 |
-
// In a real app, you'd fetch from a database
|
| 76 |
-
const chats = JSON.parse(localStorage.getItem(`chats_${req.session.user.id}`) || '[]');
|
| 77 |
-
res.json(chats);
|
| 78 |
-
});
|
| 79 |
-
|
| 80 |
-
app.post('/api/chats', async (req, res) => {
|
| 81 |
-
if (!req.session.user) return res.sendStatus(401);
|
| 82 |
-
|
| 83 |
-
// In a real app, you'd save to a database
|
| 84 |
-
const chats = JSON.parse(localStorage.getItem(`chats_${req.session.user.id}`) || '[]');
|
| 85 |
-
chats.push(req.body);
|
| 86 |
-
localStorage.setItem(`chats_${req.session.user.id}`, JSON.stringify(chats));
|
| 87 |
-
|
| 88 |
-
res.sendStatus(201);
|
| 89 |
-
});
|
| 90 |
-
|
| 91 |
-
const PORT = process.env.PORT || 3000;
|
| 92 |
-
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
| 93 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|