Skip to content

Commit

Permalink
feat: last interrupted url (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiisol authored Sep 5, 2018
1 parent 0711d95 commit 1b84285
Show file tree
Hide file tree
Showing 6 changed files with 1,926 additions and 1,863 deletions.
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { AuthService } from 'ngx-auth';
@Injectable()
export class AuthenticationService implements AuthService {

private interruptedUrl: string;

constructor(private http: Http) {}

public isAuthorized(): Observable<boolean> {
Expand Down Expand Up @@ -64,6 +66,14 @@ export class AuthenticationService implements AuthService {
return url.endsWith('refresh-token');
}

public getInterruptedUrl(): string {
return this.interruptedUrl;
}

public setInterruptedUrl(url: string): void {
this.interruptedUrl = url;
}

}
```

Expand Down Expand Up @@ -100,12 +110,12 @@ import { AuthModule, AUTH_SERVICE, PUBLIC_FALLBACK_PAGE_URI, PROTECTED_FALLBACK_
import { AuthenticationService } from './authentication.service';

@NgModule({
imports: [ AuthModule ],
providers: [
{ provide: PROTECTED_FALLBACK_PAGE_URI, useValue: '/' },
{ provide: PUBLIC_FALLBACK_PAGE_URI, useValue: '/login' },
{ provide: AUTH_SERVICE, useClass: AuthenticationService }
]
imports: [ AuthModule ],
providers: [
{ provide: PROTECTED_FALLBACK_PAGE_URI, useValue: '/' },
{ provide: PUBLIC_FALLBACK_PAGE_URI, useValue: '/login' },
{ provide: AUTH_SERVICE, useClass: AuthenticationService }
]
})
export class AuthenticationModule {

Expand All @@ -127,3 +137,29 @@ by ```ProtectedGuard``` and won't be authenticated
### Customizing authentication headers

By default, requests are intercepted and a ```{ Authorization: 'Bearer ${token}'}``` header is injected. To customize this behavior, implement the ```getHeaders``` method on your ```AuthenticationService```

### After login redirect to the interrupted URL

The `AuthService` has an optional method `setInterruptedUrl` which saves the URL that was requested before the user is redirected to the login page. This property can be used in order to redirect the user to the originally requested page after he logs in. E.g. in your `login.component.ts` (check also `AuthService` implementation above):

```typescript
@Component({
selector: 'app-login',
templateUrl: './login.component.html'
})
export class LoginComponent {

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

public login() {
this.authService
.login()
.subscribe(() =>
this.router.navigateByUrl(this.authService.getInterruptedUrl())
);
}
}
```
Loading

0 comments on commit 1b84285

Please sign in to comment.