0
votes

Hi i am new to this angular i want to setup default route in my application below is my app.routing.ts import { Routes, RouterModule } from '@angular/router';

import { HomeComponent } from './home/index';
import { LoginComponent } from './login/index';
import { RegisterComponent } from './register/index';
import { AuthGuard } from './_guards/index';

const appRoutes: Routes = [
    { path: '', component: HomeComponent, canActivate: [AuthGuard] },
    { path: 'login', component: LoginComponent },
    { path: 'register', component: RegisterComponent },

    // otherwise redirect to home
    { path: '**', redirectTo: '' }
];

export const routing = RouterModule.forRoot(appRoutes);

currently its loading LoginComponent i want to change this to RegisterComponent how can i do this can anyone help me out move forward

Below is my app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

// used to create fake backend
import { fakeBackendProvider } from './_helpers/index';

import { AppComponent }  from './app.component';
import { routing }        from './app.routing';

import { AlertComponent } from './_directives/index';
import { AuthGuard } from './_guards/index';
import { JwtInterceptor } from './_helpers/index';
import { AlertService, AuthenticationService, UserService } from './_services/index';
import { HomeComponent } from './home/index';
import { LoginComponent } from './login/index';
import { RegisterComponent } from './register/index';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpClientModule,
        routing
    ],
    declarations: [
        AppComponent,
        AlertComponent,
        HomeComponent,
        LoginComponent,
        RegisterComponent
    ],
    providers: [
        AuthGuard,
        AlertService,
        AuthenticationService,
        UserService,
        {
            provide: HTTP_INTERCEPTORS,
            useClass: JwtInterceptor,
            multi: true
        },

        // provider used to create fake backend
        fakeBackendProvider
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }
3
Is { path: '**', redirectTo: 'register' } what you're looking for? - Emin Laletovic
@eminlala I tried this { path: '**', redirectTo: 'register' } but still its loading login page - its me
What do you enter as URL when you're testing this? Also, it would be great if you could add code for AuthGuard as well. - Emin Laletovic

3 Answers

1
votes

In your AuthGard class canActivate method, in the case of a false answer, you can simply redirect the client to /register.

export class AuthGuard {

  constructor( private authService : AuthService, private router : Router ) {
  }

  canActivate( route : ActivatedRouteSnapshot, state : RouterStateSnapshot ) {
    if(this.authService.isLoggedIn()) 
      return true;
    else // This:
      this.router.navigate(['DESIRED/PATH']);
  }
}
1
votes

Please change your app routes sequence and add pathMatch attribute.

const appRoutes: Routes = [
    { path: 'login', component: LoginComponent },
    { path: 'register', component: RegisterComponent },
    { path: '', component: HomeComponent, canActivate: [AuthGuard], pathMatch: 'full' },

    // otherwise redirect to home
    { path: '**', redirectTo: '' }
];
0
votes

You can use like this,

// otherwise redirect to home
{ path: '**', redirectTo: '/' }