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

chore: sign user out if token is expired#109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanndickson merged 1 commit intomainfromethan/sign-out-on-expiry
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletionsCoder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,11 +40,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func applicationDidFinishLaunching(_: Notification) {
menuBar = .init(menuBarExtra: FluidMenuBarExtra(title: "Coder Desktop", image: "MenuBarIcon") {
VPNMenu<CoderVPNService>().frame(width: 256)
.environmentObject(self.vpn)
.environmentObject(self.state)
})
menuBar = .init(menuBarExtra: FluidMenuBarExtra(
title: "Coder Desktop",
image: "MenuBarIcon",
onAppear: {
// If the VPN is enabled, it's likely the token isn't expired
guard case .disabled = self.vpn.state, self.state.hasSession else { return }
Task { @MainActor in
await self.state.handleTokenExpiry()
}
}, content: {
VPNMenu<CoderVPNService>().frame(width: 256)
.environmentObject(self.vpn)
.environmentObject(self.state)
}
))
// Subscribe to system VPN updates
NotificationCenter.default.addObserver(
self,
Expand Down
23 changes: 23 additions & 0 deletionsCoder-Desktop/Coder-Desktop/State.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,12 @@ import CoderSDK
import Foundation
import KeychainAccess
import NetworkExtension
import os
import SwiftUI

@MainActor
class AppState: ObservableObject {
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AppState")
let appId = Bundle.main.bundleIdentifier!

// Stored in UserDefaults
Expand DownExpand Up@@ -95,6 +97,9 @@ class AppState: ObservableObject {
)
if hasSession {
_sessionToken = Published(initialValue: keychainGet(for: Keys.sessionToken))
if sessionToken == nil || sessionToken!.isEmpty == true {
clearSession()
}
}
}

Expand All@@ -105,6 +110,24 @@ class AppState: ObservableObject {
reconfigure()
}

public func handleTokenExpiry() async {
if hasSession {
let client = Client(url: baseAccessURL!, token: sessionToken!)
do {
_ = try await client.user("me")
} catch let ClientError.api(apiErr) {
// Expired token
if apiErr.statusCode == 401 {
clearSession()
}
} catch {
// Some other failure, we'll show an error if they try and do something
logger.error("failed to check token validity: \(error)")
return
}
}
}

public func clearSession() {
hasSession = false
sessionToken = nil
Expand Down
8 changes: 4 additions & 4 deletionsCoder-Desktop/CoderSDK/Client.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -104,10 +104,10 @@ public struct Client {
}

public struct APIError: Decodable, Sendable {
let response: Response
let statusCode: Int
let method: String
let url: URL
publiclet response: Response
publiclet statusCode: Int
publiclet method: String
publiclet url: URL

var description: String {
var components = ["\(method) \(url.absoluteString)\nUnexpected status code \(statusCode):\n\(response.message)"]
Expand Down
6 changes: 4 additions & 2 deletionsCoder-Desktop/project.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -92,10 +92,12 @@ packages:
url: https://github.com/SimplyDanny/SwiftLintPlugins
from: 0.57.1
FluidMenuBarExtra:
# Forked so we can dynamically update the menu bar icon.
# Forked to:
# - Dynamically update the menu bar icon
# - Set onAppear/disappear handlers.
# The upstream repo has a purposefully limited API
url: https://github.com/coder/fluid-menu-bar-extra
revision:020be37
revision:96a861a
KeychainAccess:
url: https://github.com/kishikawakatsumi/KeychainAccess
branch: e0c7eebc5a4465a3c4680764f26b7a61f567cdaf
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp