how to get a token to generate a section from login with oauth and angular 
4, most of the projects I saw are angled 2 and do not have interceptors and 
need multiple files I just need to get a token from my API
I am currently using the code as follows:
     public login (user): observable <any> {
       let body = JSON.stringify (user);


       return this.http.get (this.Jurl, body, {headers: contentHeaders})
                       // ... and calling .json () on the response to 
return data
                       .map ((res: Response) => res.json ())
                       //...errors if any
                       .catch ((error: any) => Observable.throw (error.text 
()));
      }


     // this is in login.service.ts


//this is my user.service.ts

private url: string = 'auth/login';

private _loggedIn: BehaviorSubject<boolean> = new BehaviorSubject(false);
 public loggedIn: Observable<boolean> = this._loggedIn.asObservable();

  constructor(private http: Http) { }

  autentica(usuario: Usuario) {
     return this.http
                .post(this.url, JSON.stringify(usuario))
                .map((res) => {                     
                    var token = res.headers.get('x-access-token');
                   if (token) {
                       this._loggedIn.next(true);
                       localStorage.setItem('token', token);
                   }
                });
 }

  logout() {
   localStorage.removeItem('token');
 }

  isLoggedIn() {
   let token = localStorage.getItem('token');

    if(token){ //essa atribuição é feita para atualizar a variavel e o 
resto do sistema ser notificado
     this._loggedIn.next(true);
   } else {
     this._loggedIn.next(false);
   }

    return this._loggedIn.getValue();
 }

}

// this is my login.component.ts

private router: Router,
         private authenticationService: AuthenticationService) {}

     ngOnInit () {}

     onLogin () {
         this.loading = true;
         this.authenticationService.login (this.model.user, 
this.model.password)
             .subscribe (result => {
                 if (result === true) {
                     this.router.navigate (['/ dashboard']);
                 } Else {
                     this.loading = false;
                 }
             });
     }

in the project currently I'm getting an "Access-Control-Allow-Origin" error 
does anyone know how to solve this error in my code or do you have a 
suggestion for better code organization so I do not have any more errors in 
this structure?

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to