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

Commitbf95ba9

Browse files
committed
chore: sign user out if token expired
1 parent93e7a8f commitbf95ba9

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

‎Coder Desktop/Coder Desktop/Coder_DesktopApp.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4040
}
4141

4242
func applicationDidFinishLaunching(_:Notification){
43-
menuBar=.init(menuBarExtra:FluidMenuBarExtra(title:"Coder Desktop", image:"MenuBarIcon"){
44-
VPNMenu<CoderVPNService>().frame(width:256)
45-
.environmentObject(self.vpn)
46-
.environmentObject(self.state)
47-
})
43+
menuBar=.init(menuBarExtra:FluidMenuBarExtra(
44+
title:"Coder Desktop",
45+
image:"MenuBarIcon",
46+
onAppear:{
47+
// If the VPN is enabled, it's likely the token isn't expired
48+
guard case.disabled=self.vpn.state,self.state.hasSessionelse{return}
49+
Task{@MainActorin
50+
awaitself.state.handleTokenExpiry()
51+
}
52+
}, content:{
53+
VPNMenu<CoderVPNService>().frame(width:256)
54+
.environmentObject(self.vpn)
55+
.environmentObject(self.state)
56+
}
57+
))
4858
// Subscribe to system VPN updates
4959
NotificationCenter.default.addObserver(
5060
self,

‎Coder Desktop/Coder Desktop/State.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import CoderSDK
22
import Foundation
33
import KeychainAccess
44
import NetworkExtension
5+
import os
56
import SwiftUI
67

78
@MainActor
89
classAppState:ObservableObject{
10+
privateletlogger=Logger(subsystem:Bundle.main.bundleIdentifier!, category:"AppState")
911
letappId=Bundle.main.bundleIdentifier!
1012

1113
// Stored in UserDefaults
@@ -95,6 +97,9 @@ class AppState: ObservableObject {
9597
)
9698
if hasSession{
9799
_sessionToken=Published(initialValue:keychainGet(for:Keys.sessionToken))
100+
if sessionToken==nil || sessionToken!.isEmpty==true{
101+
clearSession()
102+
}
98103
}
99104
}
100105

@@ -105,6 +110,24 @@ class AppState: ObservableObject {
105110
reconfigure()
106111
}
107112

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

‎Coder Desktop/project.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ packages:
9292
url:https://github.com/SimplyDanny/SwiftLintPlugins
9393
from:0.57.1
9494
FluidMenuBarExtra:
95-
# Forked so we can dynamically update the menu bar icon.
95+
# Forked to:
96+
# - Dynamically update the menu bar icon
97+
# - Set onAppear/disappear handlers.
9698
# The upstream repo has a purposefully limited API
9799
url:https://github.com/coder/fluid-menu-bar-extra
98-
revision:020be37
100+
revision:96a861a
99101
KeychainAccess:
100102
url:https://github.com/kishikawakatsumi/KeychainAccess
101103
branch:e0c7eebc5a4465a3c4680764f26b7a61f567cdaf

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp