Skip to content

Commit 2603ace

Browse files
chore: prompt for sign in when turning VPN on if signed out (#114)
Closes #106.
1 parent 111b30c commit 2603ace

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

Coder-Desktop/Coder-Desktop/Preview Content/PreviewVPN.swift

+2
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ final class PreviewVPN: Coder_Desktop.VPNService {
7878
func configureTunnelProviderProtocol(proto _: NETunnelProviderProtocol?) {
7979
state = .connecting
8080
}
81+
82+
var startWhenReady: Bool = false
8183
}

Coder-Desktop/Coder-Desktop/VPN/VPNService.swift

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ protocol VPNService: ObservableObject {
1010
func start() async
1111
func stop() async
1212
func configureTunnelProviderProtocol(proto: NETunnelProviderProtocol?)
13+
var startWhenReady: Bool { get set }
1314
}
1415

1516
enum VPNServiceState: Equatable {

Coder-Desktop/Coder-Desktop/Views/VPNMenu.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ struct VPNMenu<VPN: VPNService>: View {
44
@EnvironmentObject var vpn: VPN
55
@EnvironmentObject var state: AppState
66
@Environment(\.openSettings) private var openSettings
7+
@Environment(\.openWindow) private var openWindow
78

89
let inspection = Inspection<Self>()
910

@@ -16,7 +17,18 @@ struct VPNMenu<VPN: VPNService>: View {
1617
Toggle(isOn: Binding(
1718
get: { vpn.state == .connected || vpn.state == .connecting },
1819
set: { isOn in Task {
19-
if isOn { await vpn.start() } else { await vpn.stop() }
20+
if isOn {
21+
// Clicking the toggle while logged out should
22+
// open the login window, then start the VPN asap
23+
if !state.hasSession {
24+
vpn.startWhenReady = true
25+
openWindow(id: .login)
26+
} else {
27+
await vpn.start()
28+
}
29+
} else {
30+
await vpn.stop()
31+
}
2032
}
2133
}
2234
)) {
@@ -86,8 +98,7 @@ struct VPNMenu<VPN: VPNService>: View {
8698
}
8799

88100
private var vpnDisabled: Bool {
89-
!state.hasSession ||
90-
vpn.state == .connecting ||
101+
vpn.state == .connecting ||
91102
vpn.state == .disconnecting ||
92103
// Prevent starting the VPN before the user has approved the system extension.
93104
vpn.state == .failed(.systemExtensionError(.needsUserApproval))

Coder-Desktop/Coder-DesktopTests/Util.swift

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class MockVPNService: VPNService, ObservableObject {
2323
}
2424

2525
func configureTunnelProviderProtocol(proto _: NETunnelProviderProtocol?) {}
26+
var startWhenReady: Bool = false
2627
}
2728

2829
extension Inspection: @unchecked Sendable, @retroactive InspectionEmissary {}

Coder-Desktop/Coder-DesktopTests/VPNMenuTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct VPNMenuTests {
2323
try await ViewHosting.host(view) {
2424
try await sut.inspection.inspect { view in
2525
let toggle = try view.find(ViewType.Toggle.self)
26-
#expect(toggle.isDisabled())
26+
#expect(!toggle.isDisabled())
2727
#expect(throws: Never.self) { try view.find(text: "Sign in to use Coder Desktop") }
2828
#expect(throws: Never.self) { try view.find(button: "Sign in") }
2929
}

0 commit comments

Comments
 (0)