Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

2.5 KB
Raw Blame
import { Users } from '../../db/entities/Users.entity';
import type { UsersRepository } from '../../db/repositories/Users.repository';
import { UnitOfWork } from '../database/unit-of-work';
import { SessionsService, type SessionClient } from '../sessions/sessions.service';
import { AuthService } from './auth.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';

/**
 * The signed-in session a new one is carried on from: its account, the
 * password version it was issued under, and its id (null on a token from
 * before sessions were tracked).
 */
export interface SessionOrigin {
  userId: number;
  passwordVersion: number;
  sessionId: string | null;
}

/**
 * Signs in an account whose identity another way in has already proven: the
 * login is recorded on the account and the session minted in one transaction.
 *
 * Auth owns the users table, so a domain whose flow ends in a sign-in (the
 * native app hand-off) asks here instead of reaching for that table itself.
 */
@Injectable()
export class LoginSessionService {
  constructor(
    private readonly auth: AuthService,
    private readonly uow: UnitOfWork,
    private readonly sessions: SessionsService,
    @InjectRepository(Users) private readonly users: UsersRepository,
  ) {}

  /**
   * The session token, or null when the session it is carried on from no
   * longer stands: the account is gone, the password changed since, or that
   * session ended (a sign-out, a revocation, a password reset). A credential
   * the account took back cannot mint a new one this way.
   *
   * The new session keeps the identity provider's `sid` of the one it comes
   * from, so a back-channel logout of that provider session ends both.
   */
  async signIn(origin: SessionOrigin, remember: boolean | undefined, client?: SessionClient): Promise<string | null> {
    return this.uow.transactional(async () => {
      const passwordVersion = await this.users.getPasswordVersion(origin.userId);
      if (passwordVersion === null || passwordVersion !== origin.passwordVersion) return null;
      let oidcSid: string | null = null;
      if (origin.sessionId !== null) {
        const from = await this.sessions.findActiveOrigin(origin.sessionId, origin.userId);
        if (!from) return null;
        oidcSid = from.oidcSid;
      }
      await this.users.touchLastLogin(origin.userId);
      return this.auth.generateToken(
        { id: origin.userId, password_version: passwordVersion },
        remember,
        oidcSid !== null ? { ...client, oidcSid } : client,
      );
    });
  }
}