Auth0 Home Blog Docs

Auth0 Social connection - Wrong Http error status

lock
auth0
api
login

#1

I am currently using Auth0 for authentication in my SPA and I am storing the user_id generated by Auth0 in my own DB. The flow is the following:

  1. Email and password

When creating a user with email and password, either the user_id already exists in my DB and I can log the user in, either the user_id does not exist, which generates a 404 error status (NOT FOUND) and can lead to the creation of a new row in my DB.

enter image description here

  1. Google connection

Users can log in with Google authentication, in this case auth0 enables a connection with google. Here comes the error, instead of sending a 404 error it sends a 403 error (FORBIDDEN RESOURCE).

enter image description here

Did someone encounter the same problem? You can find my code below:

app.component.ts

  ngOnInit(): void {
    this.store.select('admin').subscribe(
      (adminState: AdminState) => {
        if (adminState.currentUserId && adminState.currentUserId !== this.currentUserId) {
          const uri = `users/${adminState.currentUserId}`;
          this.apiService.get(uri).subscribe(
            (user: User) => {
              this.store.dispatch(new AppActions.SetCurrentUser(user));
              this.router.navigate(['/']);
              this.currentUserId = adminState.currentUserId;
            },
            (error: HttpErrorResponse) => {
              if (error.status === 404) {
                console.log('Status code 404 - Resource not found');
                const newUser: UserCreateDto = {
                  clientId: adminState.currentClientId,
                  userId: adminState.currentUserId,
                };
                this.apiService.create('users', newUser).subscribe(
                  (u: User) => {
                    this.store.dispatch(new AppActions.SetCurrentUser(u));
                    this.router.navigate(['/']);
                    this.currentUserId = adminState.currentUserId;
                  }, (err) => {
                    console.log('Error creating User', err);
                    this.auth.logout();
                  }
                );
              } else if (error.status === 403) {
                console.log('Status code 403 - Forbidden resource');
                this.auth.logout();
              } else {
                this.auth.logout();
              }
            }
          );
        }
      }
    );
  }

Help would be greatly appreciated!