Skip to content

Commit

Permalink
Close change password page on success #161
Browse files Browse the repository at this point in the history
  • Loading branch information
IniZio committed Feb 22, 2024
1 parent ee76ee5 commit 865bd97
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Sources/Authgear.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public enum AuthenticationPage: String {

public enum SettingsPage: String {
case settings = "/settings"
case changePassword = "/settings/change_password"
case identity = "/settings/identities"
}

Expand Down Expand Up @@ -242,6 +243,7 @@ public class Authgear {
private static let ExpireInPercentage = 0.9

static let CodeChallengeMethod = "S256"
static let SDKRedirectURI = "authgearsdk://host/path"

let name: String
let clientId: String
Expand Down Expand Up @@ -1025,6 +1027,7 @@ public class Authgear {
path: String,
uiLocales: [String]? = nil,
colorScheme: ColorScheme? = nil,
closeOnSuccess: Bool? = false,
handler: URLCompletionHandler?
) {
let handler = handler.map { h in self.withMainQueueHandler(h) }
Expand All @@ -1045,6 +1048,9 @@ public class Authgear {
if let colorScheme = colorScheme {
queryItems.append(URLQueryItem(name: "x_color_scheme", value: colorScheme.rawValue))
}
if closeOnSuccess == true {
queryItems.append(URLQueryItem(name: "redirect_uri", value: Authgear.SDKRedirectURI))
}
urlComponents.queryItems = queryItems
let redirectURI = urlComponents.url!
self.generateURL(redirectURI: redirectURI.absoluteString) { generatedResult in
Expand All @@ -1065,14 +1071,16 @@ public class Authgear {
uiLocales: [String]? = nil,
colorScheme: ColorScheme? = nil,
wechatRedirectURI: String? = nil,
handler: VoidCompletionHandler? = nil
handler: VoidCompletionHandler? = nil,
closeOnSuccess: Bool? = false
) {
let handler = handler.map { h in withMainQueueHandler(h) }

self.generateAuthgearURL(
path: path,
uiLocales: uiLocales,
colorScheme: colorScheme
colorScheme: colorScheme,
closeOnSuccess: closeOnSuccess
) { [weak self] result in
guard let self = self else { return }

Expand All @@ -1092,9 +1100,7 @@ public class Authgear {

self.uiImplementation.openAuthorizationURL(
url: endpoint,
// Opening an arbitrary URL does not have a clear goal.
// So here we pass a placeholder redirect uri.
redirectURI: URL(string: "nocallback://host/path")!,
redirectURI: URL(string: Authgear.SDKRedirectURI)!,
// prefersEphemeralWebBrowserSession is true so that
// the alert dialog is never prompted and
// the app session token cookie is forgotten when the webview is closed.
Expand All @@ -1103,7 +1109,6 @@ public class Authgear {
self.unregisterCurrentWechatRedirectURI()
switch result {
case .success:
// This branch is unreachable.
handler?(.success(()))
case let .failure(error):
if case AuthgearError.cancel = error {
Expand All @@ -1121,13 +1126,15 @@ public class Authgear {
page: SettingsPage,
uiLocales: [String]? = nil,
colorScheme: ColorScheme? = nil,
wechatRedirectURI: String? = nil
wechatRedirectURI: String? = nil,
closeOnSuccess: Bool? = false
) {
openURL(
path: page.rawValue,
uiLocales: uiLocales,
colorScheme: colorScheme,
wechatRedirectURI: wechatRedirectURI
wechatRedirectURI: wechatRedirectURI,
closeOnSuccess: closeOnSuccess
)
}

Expand Down
9 changes: 9 additions & 0 deletions example/ios_example/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ class App: ObservableObject {
)
}

func changePassword() {
container?.open(
page: .changePassword,
colorScheme: self.colorScheme,
wechatRedirectURI: App.wechatRedirectURI,
closeOnSuccess: true
)
}

func promoteAnonymousUser() {
container?.promoteAnonymousUser(
redirectURI: App.redirectURI,
Expand Down
6 changes: 6 additions & 0 deletions example/ios_example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ struct ActionButtonList: View {
ActionButton(text: "Open Setting")
}.disabled(!configured || !loggedIn || isAnonymous)

Button(action: {
self.app.changePassword()
}) {
ActionButton(text: "Change Password")
}.disabled(!configured || !loggedIn || isAnonymous)

Button(action: {
self.app.showAuthTime()
}) {
Expand Down

0 comments on commit 865bd97

Please sign in to comment.