简体   繁体   English

角度laravel未经授权的401错误

[英]angular laravel unauthorized 401 error

Okay, I don't know what I am doing wrong, but I really tried everything. 好的,我不知道自己在做什么错,但是我真的尽了一切。 In agular I just send user data to sign up 在敏捷中,我只是发送用户数据进行注册

signUp(userData: LoginData) {
    return this.http.post(`${this.rootPath}api/signup`, userData);
  }

On backend 在后端

public function login()
    {
        $credentials = request(['email', 'password']);

        if (! $token = auth()->attempt($credentials)) {
            return response()->json(['error' => 'Email or password doesn\'t exist'],
                401);
        }

        return $this->respondWithToken($token);
    }

    public function signup(signUp $request)
    {
        User::create($request->all());
        return $this->login();
    }

signUp 注册

public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required'
        ];
    }

It adds new user to database, but I ALWAYS get 401 error which says user is not authorized. 它将新用户添加到数据库,但是我总是收到401错误,该错误表示用户未获得授权。 It is strange and I can't unerstand what am I doing wrong... Field REMEMBER_TOKEN is null, but I think it is not the problem... 很奇怪,我无法理解我在做什么错...字段REMEMBER_TOKEN为空,但我认为这不是问题...

I'd like to add info in case it is useful for other people. 我想添加信息以防其他人使用。 I have fixed the issue that is related to this. 我已经解决了与此有关的问题。 I am working in Angular 7, and Laravel 5.7 as API REST. 我正在使用Angular 7和Laravel 5.7作为API REST。 In angular I created an interceptor in order to add the headers 'automatically'. 在angular中,我创建了一个拦截器,以便“自动”添加标题。 And then, I've created the API for private routes and also experienced these issues described above. 然后,我为专用路由创建了API,并且还遇到了上述这些问题。 POSTMAN and Advanced REST worked well, the problem were the requests by Angular. POSTMAN和Advanced REST运作良好,问题出在Angular的要求。 And now, I've added the headers manually in each HTTP request as follows; 现在,我在每个HTTP请求中手动添加了标头,如下所示;

//Example for 'GET' request //“ GET”请求的示例

userLogOut(): Observable<any[]> {
    let parameters = new HttpHeaders();
    parameters = parameters.set('Authorization', "Bearer " + this.session.getToken());
    return this.http.get<any[]>(this.usersUrl + 'logout', { headers: parameters });
}

//Example for 'POST' request //“ POST”请求的示例

userLogOut(): Observable<any[]> {
    let parameters = new HttpHeaders();
    parameters = parameters.set('Authorization', "Bearer " + this.session.getToken() );
    return this.http.post<any[]>(this.usersUrl + 'logout', '', { headers: parameters } );
}

Both of them work well. 他们两个都工作良好。 Don't forget to add this at your constructor 不要忘记在构造函数中添加它

private http: HttpClient 

and

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM