मैं अपने ऐप में एक ऑथ गार्ड को लागू करने की कोशिश कर रहा हूं। अर्थात; केवल प्रमाणित उपयोगकर्ता ही मेरे ऐप के कुछ मार्गों तक पहुंच सकते हैं। मैं here दिए गए टट का पालन कर रहा हूं।Angular2 सेवा सिंगलटन कैसे बनाएं?
एक बार जब उपयोगकर्ता लॉग इन हो जाता है तो मैं अपने AuthService में एक बूलियन मान बदलता हूं ताकि यह इंगित किया जा सके कि उपयोग लॉग इन है। ऐप के जीवन के माध्यम से इसे बनाए रखने की आवश्यकता है।
स्रोत कोड नीचे दिए गए:
Auth-guard.service.ts
import { Injectable } from '@angular/core';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuardService implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
console.log('Auth Guard Service: ' + this.authService.isLoggedIn);
if (this.authService.isLoggedIn) { return true; }
// Store the attempted URL for redirecting
this.authService.redirectUrl = url;
// Navigate to the login page with extras
this.router.navigate(['/admin', 'admin-login']);
return false;
}
}
auth.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { User } from '../user/shared/user.model';
import { ServiceBase } from '../../core/service.base';
import { appConfig } from '../../core/app.config';
@Injectable()
export class AuthService extends ServiceBase {
public isLoggedIn: boolean = false;
redirectUrl: string;
apiUrl: string;
constructor(private http: Http) {
super();
this.apiUrl = appConfig.apiBaseUrl + '/users/signin';
}
signin(user: User, successCallback, errorCallback) {
return this.http.post(this.apiUrl, user).subscribe(
res => {
this.isLoggedIn = true;
successCallback(res);
},
err => {
//this.isLoggedIn = false;
errorCallback(err);
}
);
}
}
login.component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { User } from '../../user/shared/user.model';
import { AuthService } from '../auth.service';
@Component({
moduleId: module.id,
templateUrl: 'login.component.html'
})
export class AdminLoginComponent implements OnInit{
user: User;
userLoginForm: FormGroup;
constructor(
private formBuilder: FormBuilder,
private authService: AuthService,
private router: Router){
this.user = new User();
}
ngOnInit(){
this.buildLoginForm();
}
buildLoginForm() {
...
}
login() {
if(!this.userLoginForm.valid) return false;
this.authService.signin(this.user,
response => {
console.log('Login Component: ' + this.authService.isLoggedIn);
this.router.navigate(['/admin', 'products']);
},
response => {}
);
}
}
कंसोल आउटपुट
XHR finished loading: POST "http://localhost:3000/api/users/signin".
login.component.ts:40 Login Component: true
auth-guard.service.ts:23 Auth Guard Service: false
संपादित करें: app.module.ts
import { NgModule } from '@angular/core';
...
import { AuthGuardService } from './auth/auth-guard.service';
import { AuthService } from './auth/auth.service';
@NgModule({
imports: [
...
AdminRoutingModule
],
exports: [
],
declarations: [
...
AdminLoginComponent,
...
],
providers: [
AuthGuardService,
AuthService,
...
],
bootstrap:[]
})
export class AdminModule {}
मैं गलत यहाँ क्या कर रहा हूँ? किसी भी सहायता की सराहना की जाएगी।
देखें सेवा सिंगलटन आप 'अपने' modules' की providers' खंड के अंदर अपने सभी सेवाओं को जोड़ने के लिए है बनाने के लिए। यदि आपने अपने घटक के 'प्रदाता' अनुभाग के अंदर सेवाओं को जोड़ा है तो उस घटक के लिए नया सेवा उदाहरण प्रारंभ किया जाएगा। – ranakrunal9
हमें दिखाएं कि आपने यह सेवाएं कहां जोड़े हैं? App.module – Milad