Authenticating
Overview
This guide shows you how to add (basic) authentication to your Uxios requests by providing a username and password, or using a token or credentials. With this quick setup, you can seamlessly access protected API endpoints that require user authentication.
Steps
// 1. Describe a `Resource` ..
var resource = Resource<LoggedIn>
// *At* a URL (as string) and
.At("https://httpbin.org/basic-auth/username/password")
// *As* a User - identified by a Username and Password
.As("username", "password");
// 2. Start fetching the Value using `Promise<Post>`
Promise<LoggedIn> promise = resource.Value;
// 3. Declare what needs to happen on success - a lambda is used here, but a method works too
promise.Then(loggedIn => Debug.Log(loggedIn.authenticated));
// 4. Declare what needs to happen on success - a lambda is used here, but a method works too
promise.Catch(exception => Debug.LogError(exception.Message));
Highlights
- The
Asmethod on a Resource helps you to describe "as what user" you want to interact with the Resource - You can chain a series of options that describe the Resource (
AtandAsin this case)
Quick Tips
- You can also pass an instance of
Credentialsto theAsmethod, for example aBearerTokenCredential. - The
Exceptionin theCatchpart of the promise is an instance ofKindMen.Uxios.Error, you can get more information there. - Authentication failures also end up in the
Catchsection - and they can easily be identified by their type:KindMen.Uxios.Errors.Http.AuthenticationErroror its subtypes for even more specificity.