Skip to content

Commit 30402ce

Browse files
committed
Add Identity.verifyLoggedIn
1 parent 14b8a78 commit 30402ce

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

src/common/session.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Context } from '@nestjs/graphql';
99
import { type DateTime } from 'luxon';
1010
import { Identity } from '~/core/authentication';
1111
import { type ScopedRole } from '../components/authorization/dto';
12-
import { UnauthenticatedException } from './exceptions';
1312
import { type ID } from './id-field';
1413

1514
export interface Session {
@@ -32,13 +31,6 @@ export interface Session {
3231
};
3332
}
3433

35-
export function loggedInSession(session: Session): Session {
36-
if (session.anonymous) {
37-
throw new UnauthenticatedException('User is not logged in');
38-
}
39-
return session;
40-
}
41-
4234
@Injectable()
4335
export class SessionPipe implements PipeTransform {
4436
constructor(private readonly identity: Identity) {}
@@ -49,8 +41,7 @@ export class SessionPipe implements PipeTransform {
4941
}
5042

5143
/** @deprecated */
52-
export const LoggedInSession = () =>
53-
AnonSession({ transform: loggedInSession });
44+
export const LoggedInSession = () => AnonSession();
5445

5546
/** @deprecated */
5647
export const AnonSession =

src/components/authentication/session.interceptor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
type Session,
2222
UnauthenticatedException,
2323
} from '~/common';
24-
import { loggedInSession as verifyLoggedIn } from '~/common/session';
2524
import { ConfigService } from '~/core';
25+
import { Identity } from '~/core/authentication';
2626
import { GlobalHttpHook, type IRequest } from '~/core/http';
2727
import { rolesForScope } from '../authorization/dto';
2828
import { Anonymous } from './anonymous.decorator';
@@ -36,6 +36,7 @@ export class SessionInterceptor implements NestInterceptor {
3636
private readonly auth: AuthenticationService & {},
3737
private readonly config: ConfigService,
3838
private readonly sessionHost: SessionHost,
39+
private readonly identity: Identity,
3940
) {}
4041

4142
private readonly sessionByRequest = new AsyncLocalStorage<
@@ -85,7 +86,7 @@ export class SessionInterceptor implements NestInterceptor {
8586
Anonymous.get(executionContext.getClass()) ??
8687
!isMutation;
8788
if (!allowAnonymous && session) {
88-
verifyLoggedIn(session);
89+
this.identity.verifyLoggedIn();
8990
}
9091

9192
return next.handle();

src/components/comments/comment-thread.resolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
22
import { stripIndent } from 'common-tags';
3-
import { AnonSession, type ID, IdArg, ListArg, type Session } from '~/common';
4-
import { loggedInSession as verifyLoggedIn } from '~/common/session';
3+
import { type ID, IdArg, ListArg } from '~/common';
54
import { Loader, type LoaderOf, ResourceLoader } from '~/core';
5+
import { Identity } from '~/core/authentication';
66
import { UserLoader } from '../user';
77
import { User } from '../user/dto';
88
import { CommentThreadLoader } from './comment-thread.loader';
@@ -23,6 +23,7 @@ export class CommentThreadResolver {
2323
constructor(
2424
private readonly service: CommentService,
2525
private readonly resources: ResourceLoader,
26+
private readonly identity: Identity,
2627
) {}
2728

2829
@Query(() => CommentThread, {
@@ -41,11 +42,10 @@ export class CommentThreadResolver {
4142
async commentThreads(
4243
@IdArg({ name: 'resource' }) resourceId: ID,
4344
@ListArg(CommentThreadListInput) input: CommentThreadListInput,
44-
@AnonSession() session: Session,
4545
@Loader(CommentThreadLoader) commentThreads: LoaderOf<CommentThreadLoader>,
4646
): Promise<CommentThreadList> {
4747
// TODO move to auth policy
48-
verifyLoggedIn(session);
48+
this.identity.verifyLoggedIn();
4949
const resource = await this.service.loadCommentable(resourceId);
5050
const list = await this.service.listThreads(resource, input);
5151
commentThreads.primeAll(list.items);

src/components/file/file-url.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Response,
1111
} from '@nestjs/common';
1212
import { type ID } from '~/common';
13-
import { loggedInSession as verifyLoggedIn } from '~/common/session';
13+
import { Identity } from '~/core/authentication';
1414
import { HttpAdapter, type IRequest, type IResponse } from '~/core/http';
1515
import { SessionInterceptor } from '../authentication/session.interceptor';
1616
import { FileService } from './file.service';
@@ -23,6 +23,7 @@ export class FileUrlController {
2323
@Inject(forwardRef(() => FileService))
2424
private readonly files: FileService & {},
2525
private readonly sessionHost: SessionInterceptor,
26+
private readonly identity: Identity,
2627
private readonly http: HttpAdapter,
2728
) {}
2829

@@ -37,7 +38,7 @@ export class FileUrlController {
3738

3839
if (!node.public) {
3940
const session = await this.sessionHost.hydrateSession({ request });
40-
verifyLoggedIn(session);
41+
this.identity.verifyLoggedIn(session);
4142
}
4243

4344
// TODO authorization using session

src/core/authentication/identity.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { forwardRef, Inject, Injectable } from '@nestjs/common';
2-
import type { ID, Role } from '~/common';
2+
import { type ID, type Role, UnauthenticatedException } from '~/common';
33
import { type AuthenticationService } from '../../components/authentication';
44
import { SessionHost } from '../../components/authentication/session.host';
55

@@ -36,6 +36,15 @@ export class Identity {
3636
return this.current.anonymous;
3737
}
3838

39+
/**
40+
* Manually verify the current requestor is logged in.
41+
*/
42+
verifyLoggedIn(session?: Identity['current']) {
43+
if ((session ?? this.current).anonymous) {
44+
throw new UnauthenticatedException('User is not logged in');
45+
}
46+
}
47+
3948
/**
4049
* Is the current user an admin?
4150
*

0 commit comments

Comments
 (0)