Skip to content

chore: prompt for sign in when turning VPN on if signed out #114

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all 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
2 changes: 2 additions & 0 deletions Coder-Desktop/Coder-Desktop/Preview Content/PreviewVPN.swift
Original file line number Diff line number Diff line change
@@ -78,4 +78,6 @@ final class PreviewVPN: Coder_Desktop.VPNService {
func configureTunnelProviderProtocol(proto _: NETunnelProviderProtocol?) {
state = .connecting
}

var startWhenReady: Bool = false
}
1 change: 1 addition & 0 deletions Coder-Desktop/Coder-Desktop/VPN/VPNService.swift
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ protocol VPNService: ObservableObject {
func start() async
func stop() async
func configureTunnelProviderProtocol(proto: NETunnelProviderProtocol?)
var startWhenReady: Bool { get set }
}

enum VPNServiceState: Equatable {
17 changes: 14 additions & 3 deletions Coder-Desktop/Coder-Desktop/Views/VPNMenu.swift
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ struct VPNMenu<VPN: VPNService>: View {
@EnvironmentObject var vpn: VPN
@EnvironmentObject var state: AppState
@Environment(\.openSettings) private var openSettings
@Environment(\.openWindow) private var openWindow

let inspection = Inspection<Self>()

@@ -16,7 +17,18 @@ struct VPNMenu<VPN: VPNService>: View {
Toggle(isOn: Binding(
get: { vpn.state == .connected || vpn.state == .connecting },
set: { isOn in Task {
if isOn { await vpn.start() } else { await vpn.stop() }
if isOn {
// Clicking the toggle while logged out should
// open the login window, then start the VPN asap
if !state.hasSession {
vpn.startWhenReady = true
openWindow(id: .login)
} else {
await vpn.start()
}
} else {
await vpn.stop()
}
}
}
)) {
@@ -86,8 +98,7 @@ struct VPNMenu<VPN: VPNService>: View {
}

private var vpnDisabled: Bool {
!state.hasSession ||
vpn.state == .connecting ||
vpn.state == .connecting ||
vpn.state == .disconnecting ||
// Prevent starting the VPN before the user has approved the system extension.
vpn.state == .failed(.systemExtensionError(.needsUserApproval))
1 change: 1 addition & 0 deletions Coder-Desktop/Coder-DesktopTests/Util.swift
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ class MockVPNService: VPNService, ObservableObject {
}

func configureTunnelProviderProtocol(proto _: NETunnelProviderProtocol?) {}
var startWhenReady: Bool = false
}

extension Inspection: @unchecked Sendable, @retroactive InspectionEmissary {}
2 changes: 1 addition & 1 deletion Coder-Desktop/Coder-DesktopTests/VPNMenuTests.swift
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ struct VPNMenuTests {
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
let toggle = try view.find(ViewType.Toggle.self)
#expect(toggle.isDisabled())
#expect(!toggle.isDisabled())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The toggle is now enabled when signed out.

#expect(throws: Never.self) { try view.find(text: "Sign in to use Coder Desktop") }
#expect(throws: Never.self) { try view.find(button: "Sign in") }
}
PHP-Proxy

PHP-Proxy

Error accessing resource: 404 - Not Found