0.1.0 • Published 2 years ago

@lemonldap-ng/fetch v0.1.0

Weekly downloads
-
License
GPL-3.0
Repository
-
Last release
2 years ago

@lemonldap-ng/fetch

Provide a fetch() like function but which catches LemonLDAP::NG rejection when session is expired. When session is expired, it redirects user to the portal to renew its authentication.

Synopsis

import llngFetch from '@lemonldap-ng/fetch'

// use it like windows.fetch
llngFetch(url)
  .then( res => res.json() )
  .then( data => console.log(data) )

Description

llngFetch() acts like window.fetch(). Differences:

  • if LemonLDAP::NG portal ask for authentication, llngFetch() automatically replaces window.location to call the portal with the current URL as return URL. It adds a "authRequired" boolean value to the request:
llngFetch(url)
  .then(res => {
    if (res.authRequired) {
      console.log("Let's wait for the Portal call")
    } else if (res.ok) {
      res.json().then( data => {
        console.log('result', data)
      })
    }
  })
  • if Lemonldap::NG handler refused request with a 403 code, llngFetch() doesn't change anything except that it adds a "forbidden" boolean value to the request:
llngFetch(url)
  .then(res => {
    if (res.forbidden) {
      alert("You're not authorized to see this")
    } else if (res.ok) {
      res.json().then( data => {
        console.log('result', data)
      })
    }
  })

Replacing window.fetch

You can replace window.fetch globally to avoid rewriting an existing application:

import llngFetch from '@lemonldap-ng/fetch'
delete window.fetch
window.fetch = llngFetch