Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ember-simple-auth): add generic type argument to session #2902

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading