Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 308d677

Browse filesBrowse files
authored
Merge pull request aviabird#90 from aviabird/social_login
Adding Google login
2 parents 6c1798c + 7b27291 commit 308d677
Copy full SHA for 308d677

11 files changed

+15,249-35Lines changed: 15249 additions & 35 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎package-lock.json‎

Copy file name to clipboardExpand all lines: package-lock.json
+15,148Lines changed: 15148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"core-js": "^2.4.1",
3636
"font-awesome": "^4.7.0",
3737
"immutable": "^3.8.1",
38+
"ng2-ui-auth": "^8.0.1",
3839
"ngx-bootstrap": "^1.7.1",
3940
"reselect": "^2.5.4",
4041
"rxjs": "^5.5.2",
Collapse file

‎src/app/app.module.ts‎

Copy file name to clipboardExpand all lines: src/app/app.module.ts
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { myAuthConfig } from './oauth_config';
2+
import { Ng2UiAuthModule} from 'ng2-ui-auth';
13
import { EffectsModule } from '@ngrx/effects';
24
import { environment } from './../environments/environment';
35
import { BrowserModule } from '@angular/platform-browser';
@@ -60,15 +62,17 @@ import 'rxjs/add/observable/of';
6062
*
6163
* See: https://github.com/ngrx/platform/blob/master/docs/effects/api.md#forroot
6264
*/
63-
EffectsModule.forRoot([]),
65+
// EffectsModule.forRoot([]),
6466

6567
BrowserModule,
6668
FormsModule,
6769
HttpModule,
6870
HomeModule,
6971
LayoutModule,
7072
CoreModule,
71-
SharedModule
73+
SharedModule,
74+
Ng2UiAuthModule.forRoot(myAuthConfig)
75+
7276
],
7377
providers: [],
7478
bootstrap: [AppComponent]
Collapse file

‎src/app/auth/actions/auth.actions.ts‎

Copy file name to clipboardExpand all lines: src/app/auth/actions/auth.actions.ts
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Action } from '@ngrx/store';
2-
32
export class AuthActions {
43
static LOGIN = 'LOGIN';
54
static LOGIN_SUCCESS = 'LOGIN_SUCCESS';
65
static LOGOUT = 'LOGOUT';
76
static LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';
87
static AUTHORIZE = 'AUTHORIZE';
8+
static O_AUTH_LOGIN = 'O_AUTH_LOGIN';
9+
static NO_OP = 'NO_OPERATION'
910

1011
authorize() {
1112
return { type: AuthActions.AUTHORIZE };
@@ -15,8 +16,15 @@ export class AuthActions {
1516
return { type: AuthActions.LOGIN };
1617
}
1718

19+
oAuthLogin(provider: string) {
20+
return {
21+
type: AuthActions.O_AUTH_LOGIN,
22+
payload: provider
23+
};
24+
}
25+
1826
loginSuccess() {
19-
return { type: AuthActions.LOGIN_SUCCESS};
27+
return { type: AuthActions.LOGIN_SUCCESS };
2028
}
2129

2230
logout() {
@@ -26,4 +34,8 @@ export class AuthActions {
2634
logoutSuccess() {
2735
return { type: AuthActions.LOGOUT_SUCCESS };
2836
}
37+
38+
noOp() {
39+
return { type: AuthActions.NO_OP };
40+
}
2941
}
Collapse file

‎src/app/auth/components/login/login.component.html‎

Copy file name to clipboardExpand all lines: src/app/auth/components/login/login.component.html
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h3 class="panel-title">Login to {{title}}</h3>
1313
<span class="header-sprite login-fb-logo"></span>
1414
FACEBOOK
1515
</button>
16-
<button class="login-google login-button coming-soon" id="gPlusLogin">
16+
<button class="login-google login-button" id="gPlusLogin" (click)="socialLogin('google')">
1717
<span class="header-sprite login-gplus-logo"></span>
1818
GOOGLE
1919
</button>
Collapse file

‎src/app/auth/components/login/login.component.ts‎

Copy file name to clipboardExpand all lines: src/app/auth/components/login/login.component.ts
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AuthActions } from './../../actions/auth.actions';
12
import { Component, OnInit, OnDestroy } from '@angular/core';
23
import { environment } from '../../../../environments/environment';
34
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@@ -24,6 +25,7 @@ export class LoginComponent implements OnInit, OnDestroy {
2425
private store: Store<AppState>,
2526
private route: ActivatedRoute,
2627
private router: Router,
28+
private actions: AuthActions,
2729
private authService: AuthService
2830
) {
2931
this.redirectIfUserLoggedIn();
@@ -60,7 +62,7 @@ export class LoginComponent implements OnInit, OnDestroy {
6062
}
6163

6264
private pushErrorFor(ctrl_name: string, msg: string) {
63-
this.signInForm.controls[ctrl_name].setErrors({'msg': msg});
65+
this.signInForm.controls[ctrl_name].setErrors({ 'msg': msg });
6466
}
6567

6668
initForm() {
@@ -85,4 +87,7 @@ export class LoginComponent implements OnInit, OnDestroy {
8587
if (this.loginSubs) { this.loginSubs.unsubscribe(); }
8688
}
8789

88-
}
90+
socialLogin(provider: string) {
91+
this.store.dispatch(this.actions.oAuthLogin(provider));
92+
}
93+
}
Collapse file

‎src/app/auth/components/sign-up/sign-up.component.html‎

Copy file name to clipboardExpand all lines: src/app/auth/components/sign-up/sign-up.component.html
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ <h3 class="panel-title">Signup to {{title}}</h3>
1313
<span class="register-fb-logo register-sprite"></span>
1414
FACEBOOK
1515
</button>
16-
<button class="register-google register-button coming-soon" id="gPlusLogin"><span class="register-gplus-logo register-sprite"></span>
17-
GOOGLE
18-
</button>
16+
<button class="register-google register-button" id="gPlusLogin" (click)="socialLogin('google')">
17+
<span class="register-gplus-logo register-sprite"></span>
18+
GOOGLE
19+
</button>
1920
</div>
2021
</div>
2122
<p class="register-button-info-text register-info-text">- OR USING EMAIL -</p>
@@ -50,8 +51,7 @@ <h3 class="panel-title">Signup to {{title}}</h3>
5051
</div>
5152

5253
<div class="register-input-item">
53-
<input type="number" class="register-user-input-mobile register-user-input" name="mobile"
54-
formControlName="mobile" placeholder="Mobile Number (For order status updates)">
54+
<input type="number" class="register-user-input-mobile register-user-input" name="mobile" formControlName="mobile" placeholder="Mobile Number (For order status updates)">
5555
<div *ngIf="signUpForm.get('mobile').errors && signUpForm.get('mobile').touched">
5656
<span class="register-error-icon">!</span>
5757
<p class="register-error-message">{{signUpForm.get('mobile').errors.msg || 'Please enter a valid mobile number (10 digits)'}}</p>
Collapse file

‎src/app/auth/components/sign-up/sign-up.component.ts‎

Copy file name to clipboardExpand all lines: src/app/auth/components/sign-up/sign-up.component.ts
+23-20Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Router } from '@angular/router';
77
import { AuthService } from '../../../core/services/auth.service';
88
import { getAuthStatus } from '../../reducers/selectors';
99
import { Subscription } from 'rxjs/Subscription';
10+
import { AuthActions } from '../../actions/auth.actions';
1011

1112
@Component({
1213
selector: 'app-sign-up',
@@ -21,6 +22,7 @@ export class SignUpComponent implements OnInit, OnDestroy {
2122

2223
constructor(
2324
private fb: FormBuilder,
25+
private actions: AuthActions,
2426
private store: Store<AppState>,
2527
private router: Router,
2628
private authService: AuthService
@@ -58,7 +60,7 @@ export class SignUpComponent implements OnInit, OnDestroy {
5860
}
5961

6062
private pushErrorFor(ctrl_name: string, msg: string) {
61-
this.signUpForm.controls[ctrl_name].setErrors({'msg': msg});
63+
this.signUpForm.controls[ctrl_name].setErrors({ 'msg': msg });
6264
}
6365

6466
initForm() {
@@ -68,14 +70,14 @@ export class SignUpComponent implements OnInit, OnDestroy {
6870
const mobile = '';
6971
const gender = '';
7072

71-
this.signUpForm = this.fb.group({
72-
'email': [email, Validators.compose([Validators.required, Validators.email]) ],
73-
'password': [password, Validators.compose([Validators.required, Validators.minLength(6)]) ],
74-
'password_confirmation': [password_confirmation, Validators.compose([Validators.required, Validators.minLength(6)]) ],
75-
'mobile': [mobile, Validators.compose([Validators.required, Validators.minLength(10), Validators.maxLength(10),Validators.pattern('[0-9]{10}')]) ],
73+
this.signUpForm = this.fb.group({
74+
'email': [email, Validators.compose([Validators.required, Validators.email])],
75+
'password': [password, Validators.compose([Validators.required, Validators.minLength(6)])],
76+
'password_confirmation': [password_confirmation, Validators.compose([Validators.required, Validators.minLength(6)])],
77+
'mobile': [mobile, Validators.compose([Validators.required, Validators.minLength(10), Validators.maxLength(10), Validators.pattern('[0-9]{10}')])],
7678
'gender': [gender, Validators.required]
77-
},{validator: this.matchingPasswords('password', 'password_confirmation')}
78-
);
79+
}, { validator: this.matchingPasswords('password', 'password_confirmation') }
80+
);
7981
}
8082

8183
redirectIfUserLoggedIn() {
@@ -89,20 +91,21 @@ export class SignUpComponent implements OnInit, OnDestroy {
8991
ngOnDestroy() {
9092
if (this.registerSubs) { this.registerSubs.unsubscribe(); }
9193
}
92-
94+
9395
matchingPasswords(passwordKey: string, confirmPasswordKey: string) {
94-
return (group: FormGroup): {[key: string]: any} => {
95-
let password = group.controls[passwordKey];
96-
let confirmPassword = group.controls[confirmPasswordKey];
97-
98-
if (password.value !== confirmPassword.value) {
99-
return {
100-
mismatchedPasswords: true
101-
};
96+
return (group: FormGroup): { [key: string]: any } => {
97+
let password = group.controls[passwordKey];
98+
let confirmPassword = group.controls[confirmPasswordKey];
99+
100+
if (password.value !== confirmPassword.value) {
101+
return {
102+
mismatchedPasswords: true
103+
};
104+
}
102105
}
103106
}
104-
}
105107

106-
107-
108+
socialLogin(provider: string) {
109+
this.store.dispatch(this.actions.oAuthLogin(provider));
110+
}
108111
}
Collapse file

‎src/app/auth/effects/auth.effects.ts‎

Copy file name to clipboardExpand all lines: src/app/auth/effects/auth.effects.ts
+17-2Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Effect, Actions } from '@ngrx/effects';
2+
import { Actions, Effect} from '@ngrx/effects';
33

44
import { Action } from '@ngrx/store';
55
import { AuthService } from '../../core/services/auth.service';
@@ -16,9 +16,24 @@ export class AuthenticationEffects {
1616
) { }
1717

1818
@Effect()
19-
Authorized$: Observable<Action> = this.actions$
19+
Authorized$: Observable<Action> = this.actions$
2020
.ofType(AuthActions.AUTHORIZE)
2121
.switchMap(() => this.authService.authorized())
2222
.filter((data) => !data.error && data.count)
2323
.map(() => this.authActions.loginSuccess());
24+
25+
@Effect()
26+
OAuthLogin: Observable<Action> = this.actions$
27+
.ofType(AuthActions.O_AUTH_LOGIN)
28+
.switchMap((action: any) => {
29+
return this.authService.socialLogin(action.payload);
30+
})
31+
.filter(data => data !== null)
32+
.map((data) => {
33+
if (typeof (data) === typeof ('string')) {
34+
return this.authActions.noOp();
35+
} else {
36+
return this.authActions.loginSuccess();
37+
}
38+
});
2439
}
Collapse file

‎src/app/core/services/auth.service.ts‎

Copy file name to clipboardExpand all lines: src/app/core/services/auth.service.ts
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HttpService } from './http';
66
import { AppState } from '../../interfaces';
77
import { Store } from '@ngrx/store';
88
import { AuthActions } from '../../auth/actions/auth.actions';
9+
import { AuthService as OauthService } from 'ng2-ui-auth';
910

1011
@Injectable()
1112
export class AuthService {
@@ -21,7 +22,8 @@ export class AuthService {
2122
constructor(
2223
private http: HttpService,
2324
private actions: AuthActions,
24-
private store: Store<AppState>
25+
private store: Store<AppState>,
26+
private oAuthService: OauthService
2527
) {
2628

2729
}
@@ -138,4 +140,18 @@ export class AuthService {
138140
const jsonData = JSON.stringify(user_data);
139141
localStorage.setItem('user', jsonData);
140142
}
143+
144+
socialLogin(provider: string) {
145+
return this.oAuthService.authenticate(provider).map((res: Response) => {
146+
this.setTokenInLocalStorage(res);
147+
return res;
148+
}).catch((res: Response) => {
149+
this.http.loading.next({
150+
loading: false,
151+
hasError: true,
152+
hasMsg: `Could not login with ${provider}. Error: ${res}`
153+
});
154+
return Observable.of('Social login failed');
155+
});
156+
}
141157
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.