Skip to content

Commit 432edcc

Browse files
committed
feat(ember-simple-auth): add generic type argument to session
1 parent 5387ae2 commit 432edcc

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/ember-simple-auth/src/services/session.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ function assertSetupHasBeenCalled(isSetupCalled: boolean) {
2626

2727
type RouteOrCallback = string | (() => void);
2828

29-
type InternalSessionMock = {
29+
type InternalSessionMock<Data> = {
3030
isAuthenticated: boolean;
31-
content: { authenticated: Record<string, string> };
31+
content: Data;
3232
store: unknown;
3333
attemptedTransition: null;
3434
on: (event: 'authenticationSucceeded' | 'invalidationSucceeded', cb: () => void) => void;
@@ -40,6 +40,13 @@ type InternalSessionMock = {
4040
set(key: string, value: any): void;
4141
};
4242

43+
export type DefaultDataShape = {
44+
authenticated: {
45+
authenticator: string;
46+
[key: string]: string;
47+
};
48+
};
49+
4350
/**
4451
__The session service provides access to the current session as well as
4552
methods to authenticate it, invalidate it, etc.__ It is the main interface for
@@ -59,13 +66,13 @@ type InternalSessionMock = {
5966
@extends Service
6067
@public
6168
*/
62-
export default class SessionService extends Service {
63-
session: InternalSessionMock;
69+
export default class SessionService<Data = DefaultDataShape> extends Service {
70+
session: InternalSessionMock<Data>;
6471

6572
constructor(owner: any) {
6673
super(owner);
6774

68-
this.session = owner.lookup('session:main') as InternalSessionMock;
75+
this.session = owner.lookup('session:main');
6976
}
7077

7178
/**

packages/test-app/app/controllers/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ import type SessionService from 'test-app/services/session';
44

55
export default class ApplicationIndexController extends Controller {
66
@service declare session: SessionService;
7+
8+
constructor(owner: any) {
9+
super(owner);
10+
11+
console.log(this.session.data.authenticated.id);
12+
}
713
}

packages/test-app/app/services/session.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { inject as service } from '@ember/service';
22
import Session from 'ember-simple-auth/services/session';
33

4-
export default class SessionService extends Session {
4+
type Data = {
5+
authenticated: {
6+
id: string;
7+
};
8+
};
9+
10+
export default class SessionService extends Session<Data> {
511
@service sessionAccount: any;
612

713
handleAuthentication(routeAfterInvalidation: string) {

0 commit comments

Comments
 (0)