Skip to content

Commit

Permalink
Merge pull request #2902 from mainmatter/feat-add-generic-data-argument
Browse files Browse the repository at this point in the history
feat(ember-simple-auth): add generic type argument to session
  • Loading branch information
BobrImperator authored Dec 30, 2024
2 parents 5387ae2 + 159233e commit e85db01
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ import AdaptiveStore from 'ember-simple-auth/session-stores/adaptive';
export default class SessionStore extends AdaptiveStore {}
```

#### Optional Generic `Data` argument.

```ts
import Service from 'ember-simple-auth/services/session';

type Data = {
authenticated: {
// Any data your authenticators return
id: string;
}
}

export default class SessionService<Data> extends Service {}
```

then __the session service can be injected wherever
needed in the application__. In order to display login/logout buttons depending
on the current session state, inject the service into the respective controller
Expand Down
17 changes: 12 additions & 5 deletions packages/ember-simple-auth/src/services/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function assertSetupHasBeenCalled(isSetupCalled: boolean) {

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

type InternalSessionMock = {
type InternalSessionMock<Data> = {
isAuthenticated: boolean;
content: { authenticated: Record<string, string> };
content: Data;
store: unknown;
attemptedTransition: null;
on: (event: 'authenticationSucceeded' | 'invalidationSucceeded', cb: () => void) => void;
Expand All @@ -40,6 +40,13 @@ type InternalSessionMock = {
set(key: string, value: any): void;
};

export type DefaultDataShape = {
authenticated: {
authenticator: string;
[key: string]: string;
};
};

/**
__The session service provides access to the current session as well as
methods to authenticate it, invalidate it, etc.__ It is the main interface for
Expand All @@ -59,13 +66,13 @@ type InternalSessionMock = {
@extends Service
@public
*/
export default class SessionService extends Service {
session: InternalSessionMock;
export default class SessionService<Data = DefaultDataShape> extends Service {
session: InternalSessionMock<Data>;

constructor(owner: any) {
super(owner);

this.session = owner.lookup('session:main') as InternalSessionMock;
this.session = owner.lookup('session:main');
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/test-app/app/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ import type SessionService from 'test-app/services/session';

export default class ApplicationIndexController extends Controller {
@service declare session: SessionService;

constructor(owner: any) {
super(owner);

console.log(this.session.data.authenticated.id);
}
}
8 changes: 7 additions & 1 deletion packages/test-app/app/services/session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { inject as service } from '@ember/service';
import Session from 'ember-simple-auth/services/session';

export default class SessionService extends Session {
type Data = {
authenticated: {
id: string;
};
};

export default class SessionService extends Session<Data> {
@service sessionAccount: any;

handleAuthentication(routeAfterInvalidation: string) {
Expand Down

0 comments on commit e85db01

Please sign in to comment.