aurelia-contrib / aurelia-open-id-connect

An aurelia adapter for the IdentityModel/oidc-client-js
https://zamboni-app.azurewebsites.net
MIT License
54 stars 18 forks source link

Newbie question #25

Closed stuartbloom closed 6 years ago

stuartbloom commented 7 years ago

Hi there, and thank you for this plugin.

Although I am getting compilation errors when running the cloned demo, I am able to get it to connect ot my token server and make api calls :)

Just wondering; is it possible to use the plugin and forward the application to the token server when the application starts?

Thanks again

shaunluttin commented 7 years ago

Yes, that would be possible.

stuartbloom commented 7 years ago

@shaunluttin Thant's great, would you have an example?

shaunluttin commented 7 years ago

Hmm. I don't have an example of that off the top of my head. I've been away from this project for some time now. We can chat about it in this issue, and you'll need to do a lot of the leg work yourself while I can guide you in the right direction.

On application startup, to forward the app to the authorization service, you will need to trigger this:

https://github.com/shaunluttin/aurelia-open-id-connect/blob/master/src/open-id-connect-navigation-strategies.ts#L15-L18

    public login(instruction: NavigationInstruction): Promise<any> {
        let args: any = {};
        return this.userManager.signinRedirect(args);
    }

That is exactly what the existing login route does.

https://github.com/shaunluttin/aurelia-open-id-connect/blob/master/src/open-id-connect-routing.ts#L27-L42

    private addLoginRoute(routerConfiguration: RouterConfiguration) {
        routerConfiguration.mapRoute({
            name: "login",
            nav: false,
            navigationStrategy: (instruction: NavigationInstruction) => {
                instruction.config.redirect = "";
                return this.openIdConnectNavigationStrategies.login(instruction);
            },
            route: "login",
            settings: {
                roles: [
                    OpenIdConnectRoles.Anonymous,
                ],
            },
        });
    }

So, you need with to call openIdConnectNavigationStrategies.login(instruction) directly or trigger the configured login route.

Does that make sense?

stuartbloom commented 7 years ago

Thanks, away from desk at the moment, but will have a play after the weekend, have a good one ;)

AndreSteenbergen commented 7 years ago

Something like this? (in app.js) and place the aurelia stuff in a div, instead of body. usermanager adds an iframe, which it needs to check the session.

so like this:

<body>
      <div aurelia-app="main"></div>
      <script src="scripts/vendor-bundle.js" data-main="aurelia-bootstrapper"></script>
  </body>
import { inject } from 'aurelia-dependency-injection';
import oidcConfig from "./open-id-connect-configuration";
import { OpenIdConnect, OpenIdConnectRoles } from "aurelia-open-id-connect";

@inject(OpenIdConnect)
export class App {
    constructor(openIdConnect) {
        let self = this;
        this.openIdConnect = openIdConnect;

        let mgr = self.openIdConnect.userManager;
        self.openIdConnect.userManager.getUser().then((user) => {
            if (user) {
                mgr.querySessionStatus().then(
                    function () {
                        self._loadUserDetails(user);
                    },
                    function () {
                        mgr.removeUser();
                    }
                );
            }
            else {
                mgr.signinSilent().then(function (user) {
                    self._loadUserDetails(user);
                }, function () {
                    mgr.signinRedirect({});
                });
            }
        });

        self.openIdConnect.userManager.events.addUserLoaded(() => {
            self.openIdConnect.userManager.getUser().then((user) => {
                self._loadUserDetails(user);
            });
        });
    }

    _loadUserDetails(user) {
        this.user = user;
    }
shaunluttin commented 7 years ago

Thank-you @AndreSteenbergen for that.

AndreSteenbergen commented 7 years ago

You're welcome

Op 7 jun. 2017 19:38 schreef "Shaun Luttin" notifications@github.com:

Thank @AndreSteenbergen https://github.com/andresteenbergen for that.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/shaunluttin/aurelia-open-id-connect/issues/25#issuecomment-306869746, or mute the thread https://github.com/notifications/unsubscribe-auth/AHUjdk7KAhhln8kD05CG21yf14xPa1Kfks5sBuASgaJpZM4MtFdS .