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

Commit15066a3

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

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Foundation
33
import KeychainAccess
44
import NetworkExtension
55
import SwiftUI
6+
import CoderSDK
67

8+
@MainActor
79
classAppState:ObservableObject{
810
letappId=Bundle.main.bundleIdentifier!
911

@@ -94,6 +96,9 @@ class AppState: ObservableObject {
9496
)
9597
if hasSession{
9698
_sessionToken=Published(initialValue:keychainGet(for:Keys.sessionToken))
99+
if sessionToken==nil || sessionToken!.isEmpty==true{
100+
hasSession=false
101+
}
97102
}
98103
}
99104

@@ -104,6 +109,23 @@ class AppState: ObservableObject {
104109
reconfigure()
105110
}
106111

112+
publicfunc handleTokenExpiry()async{
113+
if hasSession{
114+
letclient=Client(url:self.baseAccessURL!, token:self.sessionToken!)
115+
do{
116+
_=tryawait client.user("me")
117+
}catchletClientError.api(apiErr){
118+
// Expired token
119+
if apiErr.statusCode==401{
120+
clearSession()
121+
}
122+
}catch{
123+
// Some other failure, we'll show an error if they try and do something
124+
return
125+
}
126+
}
127+
}
128+
107129
publicfunc clearSession(){
108130
hasSession=false
109131
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