I am working on wiring up angular front end to communicate with an API backend. The first thing I am going to do is complete the login auth. I am trying to set the token into local storage and I am not sure where I am going wrong. I keep getting a 400 bad gateway error.
auth.service console.log(res) does in back return the json response I expect from the api.
@Injectable()
export class AuthService {
private BASE_URL: string = 'http://127.0.0.1:8000/rest-auth';
private headers: HttpHeaders = new HttpHeaders({'Content-Type': 'application/json'});
constructor(private http: HttpClient) {}
login(user: LoginReponse): Promise<any> {
let url: string = `${this.BASE_URL}/login/`;
let headers = new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
});
return this.http.post(url, user, {headers: this.headers}).toPromise()
.then(
res => console.log(res)
);
}
login.component
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
private TOKEN_KEY = 'id_token';
public loginResponse: any = new LoginReponse()
constructor(
private auth: AuthService,
private router: Router
) {
}
onLogin(): void {
console.log('log user');
this.auth.login(this.loginResponse)
.then((loginResponse) => {
localStorage.setItem('token', loginResponse.data.token);
console.log('got past localstorage');
})
.catch((err) => {
// console.log(err);
console.log();
});
}
}
class
export class LoginReponse {
token: string;
user: {
pk: number;
username: string;
email: string;
first_name: string;
last_name: string;
};
}