0

How to do an authentication flow through a GraphQL API in a ngx-admin/Nebular app?

I find the available AuthStrategy classes are limited and there's no straightforward way to change their behaviour. The closest is NbPasswordAuthStrategy, that does an HTTP request, but I haven't figured out how to use it with a GraphQL API.

nuno
  • 250
  • 2
  • 11

2 Answers2

0

for now, authentication through a GraphQL is not available out of the box. To solve the issue you need to create a new strategy like NbPasswordAuthStrategy but your new strategy will make requests with GraphQL client instead of plaint HttpClient.

0

As a workaround to get authentication through a GraphQL API, I've subclassed NbLoginComponent and overwritten it's login() method, passing a proper GraphQL request payload to NbAuthService.authenticate(), e.g.:

  login(): void {
    this.errors = []
    this.messages = []
    this.submitted = true

    const data = {
      variables: this.user,
      query: 'mutation($username: String!, $password: String!) { login(username: $username, password: $password) { token } }',
    }
    this.service.authenticate(this.strategy, data).subscribe((result: NbAuthResult) => {
      this.submitted = false

      if (result.isSuccess()) {
        this.messages = result.getMessages()
      } else {
        this.errors = result.getErrors()
      }

      const redirect = result.getRedirect()
      if (redirect) {
        setTimeout(() => {
          return this.router.navigateByUrl(redirect)
        }, this.redirectDelay)
      }
      this.cd.detectChanges()
    })
  }
nuno
  • 250
  • 2
  • 11