ronzeidman / ng2-ui-auth-example

MIT License
34 stars 14 forks source link

(Ionic) Handling already logged in users to bypass login screen? #14

Closed donjae closed 7 years ago

donjae commented 7 years ago

Hi,

Was wondering how you would handle users with ng2-ui-auth_token already in the local storage, and bypass a login/create account screen. Is there routing logic for ionic to validate with this in ng2-ui-auth?

ronzeidman commented 7 years ago

Make the default redirect the main screen and add an auth guard that will check if the user is logged in else redirect him to the login screen

donjae commented 7 years ago

Hmm, ok. If you don't mind, I'm going to keep this question open since your package uses an auth guard with routes in angular2, and I wanted to apply it to Ionic which requires Ionic pages/deep link system. I'll post some code here and close it when I figure out a mock solution

donjae commented 7 years ago

If anyone is looking for the solution, the best way I found was found to create a page-routing service that consists of an event emitter. Keep in mind this solution works with a navController

@Injectable()
export class PageRouteService {
    routeLogin = new EventEmitter<any>();
    constructor(){
    }
}

Then create a subscriber in the app.component on the constructor:

constructor(...){
        this.pageRoute.routeLogin.subscribe((data)=>{
            console.log(data)
            this.nav.push(TabsPage, data);
        });
}

this way you can push data from the login screen to any page component you want, or in this case, the tabs page.