|
| 1 | +import express from 'express'; |
| 2 | +import session from 'express-session'; |
| 3 | +import { Modrinth, AuthScope } from 'typerinth'; |
| 4 | +import dotenv from 'dotenv'; |
| 5 | +import crypto from 'crypto'; |
| 6 | + |
| 7 | +dotenv.config(); |
| 8 | + |
| 9 | +const app = express(); |
| 10 | +const PORT = process.env.PORT || 3000; |
| 11 | + |
| 12 | +const { CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, SESSION_SECRET } = process.env; |
| 13 | + |
| 14 | +if (!CLIENT_ID || !CLIENT_SECRET || !REDIRECT_URI || !SESSION_SECRET) { |
| 15 | + throw new Error('Missing environment variables.'); |
| 16 | +} |
| 17 | + |
| 18 | +const USER_AGENT = 'MyApp/1.0'; // Replace with your app's user agent |
| 19 | + |
| 20 | +const modrinth = new Modrinth({ |
| 21 | + userAgent: USER_AGENT, |
| 22 | +}); |
| 23 | + |
| 24 | +// Session setup |
| 25 | +app.use( |
| 26 | + session({ |
| 27 | + secret: SESSION_SECRET, |
| 28 | + resave: false, |
| 29 | + saveUninitialized: true, |
| 30 | + cookie: { secure: false }, // use `cookie: { secure: true, sameSite: 'lax' }` in production behind HTTPS |
| 31 | + }) |
| 32 | +); |
| 33 | + |
| 34 | +declare module 'express-session' { |
| 35 | + interface SessionData { |
| 36 | + oauthState?: string; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Login Route |
| 41 | +app.get('/login', (req, res) => { |
| 42 | + const state = crypto.randomUUID(); // Generate unique state |
| 43 | + req.session.oauthState = state; |
| 44 | + |
| 45 | + const authUrl = modrinth.generateAuthorizationUrl( |
| 46 | + CLIENT_ID, |
| 47 | + REDIRECT_URI, |
| 48 | + [AuthScope.UserRead, AuthScope.PayoutsRead], |
| 49 | + state |
| 50 | + ); |
| 51 | + |
| 52 | + res.redirect(authUrl); |
| 53 | +}); |
| 54 | + |
| 55 | +// Callback Route |
| 56 | +app.get('/callback', async (req, res) => { |
| 57 | + const code = req.query.code as string; |
| 58 | + const state = req.query.state as string; |
| 59 | + const storedState = req.session.oauthState; |
| 60 | + |
| 61 | + if (!state || state !== storedState) { |
| 62 | + res.status(400).send('Invalid or missing state parameter.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (!code) { |
| 67 | + res.status(400).send('Missing code parameter.'); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + const token = await modrinth.getToken( |
| 73 | + code, |
| 74 | + CLIENT_ID, |
| 75 | + REDIRECT_URI, |
| 76 | + CLIENT_SECRET |
| 77 | + ); |
| 78 | + |
| 79 | + // Use the token to make API requests |
| 80 | + const user = await modrinth.getAuthUser(token.access_token); |
| 81 | + res.json({ |
| 82 | + message: 'Successfully authenticated with Modrinth!', |
| 83 | + user, |
| 84 | + }); |
| 85 | + } catch (error) { |
| 86 | + console.error('OAuth callback error:', error); |
| 87 | + res.status(500).send('Failed to authenticate.'); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +// Start server |
| 92 | +app.listen(PORT, () => { |
| 93 | + console.log(`Server is running at http://localhost:${PORT}`); |
| 94 | +}); |
0 commit comments