Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0617964

Browse files
committed
chore: sign user out on launch if token expired
1 parentb7ccbca commit0617964

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

‎Coder Desktop/Coder Desktop/Coder_DesktopApp.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5656
state.reconfigure()
5757
}
5858
}
59+
// Sign out if token is expired
60+
Task{@MainActorin
61+
await state.handleTokenExpiry()
62+
}
5963
}
6064

6165
// This function MUST eventually call `NSApp.reply(toApplicationShouldTerminate: true)`

‎Coder Desktop/Coder Desktop/State.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import KeychainAccess
44
import NetworkExtension
55
import SwiftUI
66

7+
@MainActor
78
classAppState:ObservableObject{
89
letappId=Bundle.main.bundleIdentifier!
910

@@ -94,6 +95,9 @@ class AppState: ObservableObject {
9495
)
9596
if hasSession{
9697
_sessionToken=Published(initialValue:keychainGet(for:Keys.sessionToken))
98+
if sessionToken==nil || sessionToken!.isEmpty==true{
99+
hasSession=false
100+
}
97101
}
98102
}
99103

@@ -104,6 +108,23 @@ class AppState: ObservableObject {
104108
reconfigure()
105109
}
106110

111+
publicfunc handleTokenExpiry()async{
112+
if hasSession{
113+
letclient=Client(url: baseAccessURL!, token: sessionToken!)
114+
do{
115+
_=tryawait client.user("me")
116+
}catchletClientError.api(apiErr){
117+
// Expired token
118+
if apiErr.statusCode==401{
119+
clearSession()
120+
}
121+
}catch{
122+
// Some other failure, we'll show an error if they try and do something
123+
return
124+
}
125+
}
126+
}
127+
107128
publicfunc clearSession(){
108129
hasSession=false
109130
sessionToken=nil

‎Coder Desktop/CoderSDK/Client.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public struct Client {
104104
}
105105

106106
publicstructAPIError:Decodable,Sendable{
107-
letresponse:Response
108-
letstatusCode:Int
109-
letmethod:String
110-
leturl:URL
107+
publicletresponse:Response
108+
publicletstatusCode:Int
109+
publicletmethod:String
110+
publicleturl:URL
111111

112112
vardescription:String{
113113
varcomponents=["\(method)\(url.absoluteString)\nUnexpected status code\(statusCode):\n\(response.message)"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp