|
| 1 | +# 🔐 Authentication Guide |
| 2 | + |
| 3 | +This section explains how to use the UltimateAuth client for authentication flows. |
| 4 | + |
| 5 | +## 🧠 Overview |
| 6 | + |
| 7 | +Authentication in UltimateAuth is **flow-based**, not endpoint-based. |
| 8 | + |
| 9 | +You interact with: |
| 10 | + |
| 11 | +👉 `FlowClient` |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 🔑 Login |
| 16 | + |
| 17 | +### Basic Login |
| 18 | + |
| 19 | +```csharp |
| 20 | +await UAuthClient.Flows.LoginAsync(new LoginRequest |
| 21 | +{ |
| 22 | + Identifier = "user@ultimateauth.com", |
| 23 | + Secret = "password" |
| 24 | +}); |
| 25 | +``` |
| 26 | + |
| 27 | +👉 This triggers a full login flow: |
| 28 | + |
| 29 | +- Sends credentials |
| 30 | +- Handles redirect |
| 31 | +- Establishes session |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## ⚡ Try Login (Pre-check) |
| 36 | + |
| 37 | +```csharp |
| 38 | +var result = await UAuthClient.Flows.TryLoginAsync( |
| 39 | + new LoginRequest |
| 40 | + { |
| 41 | + Identifier = "user@mail.com", |
| 42 | + Secret = "password" |
| 43 | + }, |
| 44 | + UAuthSubmitMode.TryOnly |
| 45 | +); |
| 46 | +``` |
| 47 | + |
| 48 | +### Modes |
| 49 | + |
| 50 | +| Mode | Behavior | |
| 51 | +|--------------|--------------------------------| |
| 52 | +| TryOnly | Validate only | |
| 53 | +| DirectCommit | Skip validation | |
| 54 | +| TryAndCommit | Validate then login if success | |
| 55 | + |
| 56 | +👉 Use `DirectCommit` when: |
| 57 | +- You need maximum performance with sacrifice interactive SPA capabilities. |
| 58 | + |
| 59 | +👉 Use `TryOnly` when: |
| 60 | + |
| 61 | +- You need validation feedback |
| 62 | +- You want custom UI flows |
| 63 | + |
| 64 | +👉 Use `TryAndCommit` when: |
| 65 | + |
| 66 | +- You need completely interactive SPA experience. |
| 67 | +- UltimateAuth suggests to use TryAndCommit. |
| 68 | + |
| 69 | +<br> |
| 70 | + |
| 71 | +## 🔄 Refresh |
| 72 | + |
| 73 | +```csharp |
| 74 | +var result = await UAuthClient.Flows.RefreshAsync(); |
| 75 | +``` |
| 76 | + |
| 77 | +### Possible Outcomes |
| 78 | + |
| 79 | +- Success → new tokens/session updated |
| 80 | +- Touched → session extended |
| 81 | +- Rotated → refresh token rotated |
| 82 | +- NoOp → nothing changed |
| 83 | +- ReauthRequired → login required |
| 84 | + |
| 85 | +👉 Refresh behavior depends on auth mode: |
| 86 | + |
| 87 | +- PureOpaque → session touch |
| 88 | +- Hybrid/JWT → token rotation |
| 89 | + |
| 90 | +In default, successful refresh returns success outcome. If you want to learn success detail such as no-op, touched or rotated, open it via server options: |
| 91 | + |
| 92 | +```csharp |
| 93 | +builder.Services.AddUltimateAuthServer(o => |
| 94 | +{ |
| 95 | + o.Diagnostics.EnableRefreshDetails = true; |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +<br> |
| 100 | + |
| 101 | +## 🚪 Logout |
| 102 | + |
| 103 | +```csharp |
| 104 | +await UAuthClient.Flows.LogoutAsync(); |
| 105 | +``` |
| 106 | + |
| 107 | +👉 This: |
| 108 | + |
| 109 | +- Ends current session |
| 110 | +- Clears authentication state |
| 111 | + |
| 112 | +<br> |
| 113 | + |
| 114 | +## 📱 Device Logout Variants |
| 115 | + |
| 116 | +```csharp |
| 117 | +await UAuthClient.Flows.LogoutMyDeviceAsync(sessionId); |
| 118 | +await UAuthClient.Flows.LogoutMyOtherDevicesAsync(); |
| 119 | +await UAuthClient.Flows.LogoutAllMyDevicesAsync(); |
| 120 | +``` |
| 121 | + |
| 122 | +👉 These operate on **session chains (devices)** |
| 123 | + |
| 124 | +<br> |
| 125 | + |
| 126 | +## 🔐 PKCE Flow (Public Clients) |
| 127 | + |
| 128 | +### Start PKCE |
| 129 | + |
| 130 | +```csharp |
| 131 | +await UAuthClient.Flows.BeginPkceAsync(); |
| 132 | +``` |
| 133 | + |
| 134 | +### Complete PKCE |
| 135 | + |
| 136 | +```csharp |
| 137 | +await UAuthClient.Flows.CompletePkceLoginAsync(request); |
| 138 | +``` |
| 139 | + |
| 140 | +> Complete PKCE also has try semantics the same as login flow. UltimateAuth suggests to use `TryCompletePkceLoginAsync` for complete interactive experience. |
| 141 | +
|
| 142 | +👉 Required for: |
| 143 | + |
| 144 | +- SPA |
| 145 | +- Blazor WASM |
| 146 | +- Mobile apps |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## 🚨 Security Note |
| 151 | + |
| 152 | +- Public clients MUST use PKCE |
| 153 | +- Server clients MAY allow direct login |
| 154 | + |
| 155 | +Direct credential posting disabled by default and throws exception when you directly call login. You can enable it via options. You should only use it for debugging and development purposes. |
| 156 | +```csharp |
| 157 | +builder.Services.AddUltimateAuthClientBlazor(o => |
| 158 | +{ |
| 159 | + o.Login.AllowCredentialPost = true; |
| 160 | +}); |
| 161 | +--- |
| 162 | + |
| 163 | +## 🎯 Summary |
| 164 | + |
| 165 | +Authentication in UltimateAuth: |
| 166 | + |
| 167 | +- is flow-driven |
| 168 | +- adapts to client type |
| 169 | +- enforces security by design |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +👉 Always use `FlowClient` for authentication operations |
0 commit comments