Skip to content

Commit 2b497ad

Browse files
committed
Cleanup and added template features
1 parent 0931295 commit 2b497ad

16 files changed

Lines changed: 340 additions & 79 deletions

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"type": "chrome",
99
"request": "launch",
1010
"name": "Launch Chrome against localhost",
11+
"userDataDir": false,
1112
"url": "http://localhost:4200",
1213
"webRoot": "${workspaceFolder}"
1314
}
1415
]
15-
}
16+
}

package-lock.json

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/github-auth-lib/src/lib/callback/callback.component.css

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
callback works!
3+
</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { CallbackComponent } from './callback.component';
4+
5+
describe('CallbackComponent', () => {
6+
let component: CallbackComponent;
7+
let fixture: ComponentFixture<CallbackComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ CallbackComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(CallbackComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Component, OnInit, AfterViewInit, Inject } from '@angular/core';
2+
import { ActivatedRoute, Params, Router } from '@angular/router';
3+
import { Subscription } from 'rxjs';
4+
import { filter, map } from 'rxjs/operators';
5+
import { HttpClient, HttpHeaders } from '@angular/common/http';
6+
import { LocalStorageService } from 'angular-2-local-storage';
7+
import { User } from '../models/user.model';
8+
import { IClientConfig } from '../client-config.interface';
9+
10+
@Component({
11+
selector: 'ga-callback',
12+
templateUrl: './callback.component.html',
13+
styleUrls: ['./callback.component.css']
14+
})
15+
export class CallbackComponent implements OnInit, AfterViewInit {
16+
redirectAfterLogin: string;
17+
redirectSubscription: Subscription;
18+
getCodeSubscription: Subscription;
19+
codeRedirectUrl: string;
20+
21+
constructor(
22+
@Inject('CLIENT_CONFIG') config: IClientConfig,
23+
private httpClient: HttpClient,
24+
private activatedRoute: ActivatedRoute,
25+
private localStorageService: LocalStorageService,
26+
private router: Router
27+
) {
28+
this.redirectAfterLogin = (config.redirectAfterLogin === undefined) ? '/' : config.redirectAfterLogin;
29+
this.codeRedirectUrl = config.codeRedirectUrl;
30+
}
31+
32+
ngOnInit() {
33+
}
34+
35+
ngAfterViewInit(): void {
36+
this.redirectSubscription = this.activatedRoute.queryParams.pipe(
37+
filter((params: Params) => { console.log(params); return params['code'] !== undefined }),
38+
map((params: Params) => params['code'])
39+
)
40+
.subscribe(
41+
(code: string) => {
42+
this.getCodeSubscription = this.httpClient.get(this.codeRedirectUrl + '/' + code, { responseType: 'text'}).pipe(
43+
map((response) => JSON.parse(response).token)
44+
).subscribe(
45+
(access_token: string) => {
46+
this.localStorageService.set('access_token', access_token);
47+
const options = {
48+
headers: new HttpHeaders({
49+
'Authorization': `Bearer ${access_token}`
50+
})
51+
}
52+
this.httpClient.get<User>('https://api.github.com/user', options)
53+
.subscribe(
54+
(user: User) => {
55+
this.localStorageService.set('github_user_info', JSON.stringify(user));
56+
this.router.navigate([this.redirectAfterLogin]);
57+
}
58+
);
59+
}
60+
)
61+
}
62+
)
63+
}
64+
}

projects/github-auth-lib/src/lib/github-auth-lib.module.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@ import { LocalStorageModule } from 'angular-2-local-storage';
1010
import { LocalStorageService } from 'angular-2-local-storage';
1111

1212
import { OAuthModule } from 'angular-oauth2-oidc';
13-
import { OAuthService } from 'angular-oauth2-oidc';
1413
import { ValidationHandler } from 'angular-oauth2-oidc';
1514
import { JwksValidationHandler } from 'angular-oauth2-oidc';
1615
import { UrlHelperService } from 'angular-oauth2-oidc';
1716

18-
import { OAuthFactory } from './github-auth.service';
17+
import { GithubAuthService, GithubAuthFactory } from './github-auth.service';
1918
import { StorageBrigeFactory } from './github-auth.service';
2019
import { StorageBrige } from './github-auth.service';
2120

2221
import { IClientConfig } from './client-config.interface';
2322
import { LoginComponent } from './login/login.component';
2423

25-
import { GithubAuthService } from './github-auth.service';
24+
import { CallbackComponent } from './callback/callback.component';
2625

2726
@NgModule({
2827
imports: [
@@ -38,16 +37,18 @@ import { GithubAuthService } from './github-auth.service';
3837
})
3938
],
4039
declarations: [
41-
LoginComponent
40+
LoginComponent,
41+
CallbackComponent
4242
],
4343
entryComponents: [
44-
LoginComponent
44+
LoginComponent,
45+
CallbackComponent
4546
],
4647
exports: [
4748
LoginComponent,
4849
]
4950
})
50-
export class GithubAuthLibModule {
51+
export class GithubAuthLibModule {
5152
static withConfig(clientConfig: IClientConfig): ModuleWithProviders {
5253
return {
5354
ngModule: GithubAuthLibModule,
@@ -64,9 +65,8 @@ export class GithubAuthLibModule {
6465
useFactory: StorageBrigeFactory,
6566
multi: false
6667
},
67-
68-
OAuthService, {
69-
provide: OAuthService,
68+
{
69+
provide: GithubAuthService,
7070
deps: [
7171
'CLIENT_CONFIG',
7272
Router,
@@ -76,15 +76,15 @@ export class GithubAuthLibModule {
7676
ValidationHandler,
7777
UrlHelperService
7878
],
79-
useFactory: OAuthFactory,
79+
useFactory: GithubAuthFactory,
8080
multi: false
8181
},
82-
{ provide: GithubAuthService, useExisting: OAuthService }
82+
8383
]
8484
};
8585
}
8686
}
8787

8888
export { IClientConfig } from './client-config.interface';
8989
export { GithubAuthService } from './github-auth.service';
90-
export { LoginComponent } from './login/login.component';
90+
export { LoginComponent } from './login/login.component';

projects/github-auth-lib/src/lib/github-auth.service.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { IClientConfig } from './client-config.interface';
66
import { Router } from '@angular/router';
77
import { HttpClient } from '@angular/common/http';
88
import { AuthConfig } from 'angular-oauth2-oidc';
9+
import { User } from './models/user.model';
10+
import { CallbackComponent } from './callback/callback.component';
911

1012
@Injectable()
1113
export class StorageBrige implements Storage {
@@ -57,16 +59,15 @@ export class LocalStorageBrigeInjector {
5759
}
5860

5961
@Injectable()
60-
export class OAuthInjector {
61-
private static instance: OAuthService = null;
62+
export class GithubAuthInjector {
63+
private static instance: GithubAuthService = null;
6264

63-
// Return the instance of the service
64-
public static getInstance(config: IClientConfig, router: Router, ngZone: NgZone, httpClient: HttpClient, storageBrige: StorageBrige, validationHandler: ValidationHandler, urlHelperService: UrlHelperService): OAuthService {
65-
if (OAuthInjector.instance === null) {
65+
public static getInstance(config: IClientConfig, router: Router, ngZone: NgZone, httpClient: HttpClient, storageBrige: StorageBrige, validationHandler: ValidationHandler, urlHelperService: UrlHelperService): GithubAuthService {
66+
if (GithubAuthInjector.instance === null) {
6667
router.config.push(
6768
{
6869
path: config.redirectUrl.split('/').pop(),
69-
component: LoginComponent
70+
component: CallbackComponent
7071
}
7172
);
7273
router.resetConfig(router.config);
@@ -79,21 +80,53 @@ export class OAuthInjector {
7980
authConfig.redirectUri = config.redirectUrl;
8081
authConfig.loginUrl = 'https://github.com/login/oauth/authorize';
8182

82-
OAuthInjector.instance = new OAuthService(ngZone, httpClient, storageBrige, validationHandler, authConfig, urlHelperService);
83+
GithubAuthInjector.instance = new GithubAuthService(ngZone, httpClient, storageBrige, validationHandler, authConfig, urlHelperService);
8384

8485
}
85-
return OAuthInjector.instance;
86+
return GithubAuthInjector.instance;
8687
}
8788
}
8889

8990
@Injectable()
90-
export class GithubAuthService extends OAuthService {
91+
export class GithubAuthService {
92+
private oauthService: OAuthService;
93+
constructor(
94+
private ngZone: NgZone,
95+
private httpClient: HttpClient,
96+
private storageBrige: StorageBrige,
97+
private validationHandler: ValidationHandler,
98+
private authConfig: AuthConfig,
99+
private urlHelperService: UrlHelperService,
100+
) {
101+
this.oauthService = new OAuthService(ngZone, httpClient, storageBrige, validationHandler, authConfig, urlHelperService);
102+
}
103+
104+
startImplicitFlow() {
105+
this.oauthService.initImplicitFlow();
106+
}
107+
108+
logOut() {
109+
this.oauthService.logOut();
110+
this.storageBrige.removeItem('github_user_info');
111+
}
112+
113+
getAccessToken(): string {
114+
let tokenRaw = this.oauthService.getAccessToken();
115+
if(tokenRaw !== undefined) {
116+
let token = tokenRaw.replace(/\"/g, '');
117+
return token;
118+
}
119+
}
120+
121+
getUserInfo(): User {
122+
return JSON.parse(this.storageBrige.getItem('github_user_info'));
123+
}
91124
}
92125

93-
export function OAuthFactory(config: IClientConfig, router: Router, ngZone: NgZone, httpClient: HttpClient, storageBrige: StorageBrige, validationHandler: ValidationHandler, urlHelperService: UrlHelperService): OAuthService {
94-
return OAuthInjector.getInstance(config, router, ngZone, httpClient, storageBrige, validationHandler, urlHelperService);
126+
export function GithubAuthFactory(config: IClientConfig, router: Router, ngZone: NgZone, httpClient: HttpClient, storageBrige: StorageBrige, validationHandler: ValidationHandler, urlHelperService: UrlHelperService): GithubAuthService {
127+
return GithubAuthInjector.getInstance(config, router, ngZone, httpClient, storageBrige, validationHandler, urlHelperService);
95128
}
96129

97130
export function StorageBrigeFactory(localStorageService: LocalStorageService): StorageBrige {
98131
return LocalStorageBrigeInjector.getInstance(localStorageService);
99-
}
132+
}

0 commit comments

Comments
 (0)