Describe only pending onboarding steps in access responses

This commit is contained in:
unimatrixgit 2026-11-22 11:58:36 +01:00
commit 18c66fbfcc
2 changed files with 10 additions and 3 deletions

View file

@ -12,7 +12,7 @@ export async function firstLoginState(db,user){
export async function firstLoginGate(request,db){
const path=new URL(request.url).pathname;if(!path.startsWith('/api/')&&!path.startsWith('/sso/')&&!path.startsWith('/git/'))return null;
if(['/api/auth/login','/api/auth/logout','/api/auth/session','/api/auth/password','/api/auth/terms','/api/account/email'].includes(path))return null;
const state=await firstLoginState(db,await identity(request,db));return state?Response.json({message:'Complete your first sign-in: change your password and verify your own email address.',firstLogin:state,securityUrl:'https://cp.dunerealms.com/#security'},{status:428,headers:{'Cache-Control':'no-store'}}):null;
const state=await firstLoginState(db,await identity(request,db));return state?Response.json({message:'Complete your first sign-in: '+[state.passwordRequired?'change your password':null,state.emailRequired?'verify your email address':null,state.termsRequired?'accept the terms':null].filter(Boolean).join(', ')+'.',firstLogin:state,securityUrl:'https://cp.dunerealms.com/#security'},{status:428,headers:{'Cache-Control':'no-store'}}):null;
}
export async function initializeFirstLogin(db){await db.prepare(FIRST_LOGIN_SCHEMA).run();await db.prepare('CREATE TABLE IF NOT EXISTS site_terms_acceptances(user_id CHAR(36) NOT NULL,version VARCHAR(64) NOT NULL,accepted_at BIGINT NOT NULL,PRIMARY KEY(user_id,version))').run();}

View file

@ -1,6 +1,6 @@
import test from 'node:test';import assert from 'node:assert/strict';
import test from 'node:test';import assert from 'node:assert/strict';import {createHash} from 'node:crypto';
import {LocalD1} from './d1-adapter.mjs';
import {firstLoginState,initializeFirstLogin,TERMS_VERSION} from '../node/first-login.mjs';
import {firstLoginState,firstLoginGate,initializeFirstLogin,TERMS_VERSION} from '../node/first-login.mjs';
test('no_reply explicitly waives email onboarding without claiming verified email or waiving password and terms',async()=>{
const db=new LocalD1('CREATE TABLE site_contact_emails(user_id TEXT,address TEXT,verified_at INTEGER)');
try{await initializeFirstLogin(db);db.sqlite.prepare('INSERT INTO site_first_login VALUES(?,?,?)').run('user',0,Date.now());db.sqlite.prepare('INSERT INTO site_contact_emails VALUES(?,?,NULL)').run('user','no_reply@dunerealms.com');
@ -13,3 +13,10 @@ test('no_reply explicitly waives email onboarding without claiming verified emai
db.sqlite.prepare('UPDATE site_contact_emails SET verified_at=?').run(Date.now());assert.equal(await firstLoginState(db,{id:'user'}),null);
}finally{db.close();}
});
test('the live gate describes only unfinished steps and opens normal APIs after no-reply onboarding',async()=>{
const db=new LocalD1('CREATE TABLE site_accounts(id TEXT,username TEXT,display_name TEXT,role TEXT);CREATE TABLE site_sessions(token_hash TEXT,user_id TEXT,expires_at INTEGER);CREATE TABLE site_contact_emails(user_id TEXT,address TEXT,verified_at INTEGER)');
try{await initializeFirstLogin(db);const token='x'.repeat(43);db.sqlite.prepare('INSERT INTO site_accounts VALUES(?,?,?,?)').run('user','test_citizen','Test Citizen','user');db.sqlite.prepare('INSERT INTO site_sessions VALUES(?,?,?)').run(createHash('sha256').update(token).digest('hex'),'user',Date.now()+60000);db.sqlite.prepare('INSERT INTO site_contact_emails VALUES(?,?,NULL)').run('user','no_reply@dunerealms.com');db.sqlite.prepare('INSERT INTO site_first_login VALUES(?,?,?)').run('user',1,Date.now());
const request=new Request('https://cp.dunerealms.com/api/game',{headers:{cookie:'__Host-dune_session='+token}}),pending=await firstLoginGate(request,db);assert.equal(pending.status,428);const body=await pending.json();assert.equal(body.message,'Complete your first sign-in: accept the terms.');assert.deepEqual(body.firstLogin,{passwordRequired:false,emailRequired:false,termsRequired:true});
db.sqlite.prepare('INSERT INTO site_terms_acceptances VALUES(?,?,?)').run('user',TERMS_VERSION,Date.now());assert.equal(await firstLoginGate(request,db),null);
}finally{db.close();}
});