A SvelteKit application demonstrating Discord and Telegram OAuth2 integration with JWT authentication.
GET /api/auth/discord/auth-url?redirectUri=...
- Get Discord authorization URLPOST /api/auth/connect/discord
- Connect Discord accountGET /api/auth/telegram/widget-config
- Get Telegram widget configurationPOST /api/auth/connect/telegram
- Connect Telegram accountnpm install
Copy .env.example
to .env
and configure your OAuth credentials:
cp .env.example .env
http://localhost:8000/auth/discord/callback
.env
fileOptional: For guild membership verification:
.env
file/setdomain
command to set your domain: localhost
.env
fileOptional: For channel membership verification:
.env
fileGenerate a secure JWT secret:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
Add this to your .env
file as JWT_SECRET
.
Start the development server:
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
To create a production version of your app:
npm run build
You can preview the production build with npm run preview
.
src/
├── lib/
│ ├── components/auth/ # OAuth UI components
│ ├── services/ # Business logic services
│ ├── types/ # TypeScript type definitions
│ └── config.ts # Environment configuration
├── routes/
│ ├── api/auth/ # API endpoints
│ ├── auth/ # OAuth callback pages
│ └── +page.svelte # Demo page
└── app.html # HTML template
<script>
import DiscordOAuth from '$lib/components/auth/DiscordOAuth.svelte';
import TelegramOAuth from '$lib/components/auth/TelegramOAuth.svelte';
function handleSuccess(result) {
console.log('User authenticated:', result);
// Store tokens and redirect user
}
function handleError(error) {
console.error('Authentication failed:', error);
}
</script>
<DiscordOAuth
onSuccess={handleSuccess}
onError={handleError}
referralCode="optional-referral"
/>
<TelegramOAuth
onSuccess={handleSuccess}
onError={handleError}
referralCode="optional-referral"
/>
// Get Discord auth URL
const response = await fetch('/api/auth/discord/auth-url?redirectUri=http://localhost:5173/callback');
const { authUrl } = await response.json();
// Connect Discord account
const response = await fetch('/api/auth/connect/discord', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
code: 'discord_oauth_code',
redirectUri: 'http://localhost:5173/callback',
referralCode: 'optional'
})
});
// Connect Telegram account
const response = await fetch('/api/auth/connect/telegram', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: 123456789,
first_name: 'John',
username: 'johndoe',
auth_date: 1640995200,
hash: 'telegram_hash',
referralCode: 'optional'
})
});
MIT