Skip to content

Commit

Permalink
Add snippets to SSO user if it's the first user
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-dalby committed Nov 21, 2024
1 parent 91c8375 commit e3d4109
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions server/src/routes/oidcRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { JWT_SECRET, TOKEN_EXPIRY, ALLOW_NEW_ACCOUNTS } from '../middleware/auth
import jwt from 'jsonwebtoken';
import Logger from '../logger.js';
import { getDb } from '../config/database.js';
import { up_v1_5_0_snippets } from '../config/migrations/20241117-migration.js';

const router = express.Router();

Expand Down Expand Up @@ -50,22 +51,26 @@ router.get('/callback', async (req, res) => {
return res.status(404).json({ error: 'OIDC not enabled' });
}

const db = getDb();
const userCount = db.prepare('SELECT COUNT(*) as count FROM users').get().count;
const hasUsers = userCount > 0;

const baseUrl = getBaseUrl(req);
const callbackUrl = oidc.getCallbackUrl(baseUrl);
const queryString = new URLSearchParams(req.query).toString();
const currentUrl = queryString ? `${callbackUrl}?${queryString}` : callbackUrl;

Logger.debug('Full callback URL:', currentUrl);

const { tokens, userInfo } = await oidc.handleCallback(currentUrl, callbackUrl);
const { _, userInfo } = await oidc.handleCallback(currentUrl, callbackUrl);
Logger.debug('Authentication successful');

const existingUser = await userRepository.findByOIDCId(
userInfo.sub,
oidc.config.serverMetadata().issuer
);

if (!existingUser) {
if (!hasUsers && !existingUser) {
const db = getDb();
const userCount = db.prepare('SELECT COUNT(*) as count FROM users').get().count;
const hasUsers = userCount > 0;
Expand All @@ -81,6 +86,10 @@ router.get('/callback', async (req, res) => {
oidc.config.serverMetadata().issuer
);

if (!hasUsers) {
await up_v1_5_0_snippets(db, user.id);
}

const token = jwt.sign({
id: user.id,
username: user.username
Expand Down

0 comments on commit e3d4109

Please sign in to comment.