Skip to content

Commit e85db01

Browse files
Merge pull request #2902 from mainmatter/feat-add-generic-data-argument
feat(ember-simple-auth): add generic type argument to session
2 parents 5387ae2 + 159233e commit e85db01

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ import AdaptiveStore from 'ember-simple-auth/session-stores/adaptive';
151151
export default class SessionStore extends AdaptiveStore {}
152152
```
153153

154+
#### Optional Generic `Data` argument.
155+
156+
```ts
157+
import Service from 'ember-simple-auth/services/session';
158+
159+
type Data = {
160+
authenticated: {
161+
// Any data your authenticators return
162+
id: string;
163+
}
164+
}
165+
166+
export default class SessionService<Data> extends Service {}
167+
```
168+
154169
then __the session service can be injected wherever
155170
needed in the application__. In order to display login/logout buttons depending
156171
on the current session state, inject the service into the respective controller

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)