forked from alexziskind1/nativescript-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtns-oauth-native-view-controller.ios.ts
138 lines (121 loc) · 3.71 KB
/
tns-oauth-native-view-controller.ios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Frame } from "@nativescript/core";
import {
TnsOAuthClient,
ITnsOAuthTokenResult,
TnsOAuthClientLoginBlock,
TnsOAuthClientLogoutBlock,
} from "./index";
import {
ITnsOAuthLoginController,
TnsOAuthLoginSubController,
} from "./tns-oauth-login-sub-controller";
function setup() {
@NativeClass()
class TnsOAuthLoginNativeViewController
extends NSObject
implements SFSafariViewControllerDelegate, ITnsOAuthLoginController {
public static ObjCProtocols = [SFSafariViewControllerDelegate];
private loginController: TnsOAuthLoginSubController = null;
private safariViewController: SFSafariViewController;
public static initWithClient(client: TnsOAuthClient) {
const instance = new TnsOAuthLoginNativeViewController();
if (instance) {
instance.loginController = new TnsOAuthLoginSubController(client);
}
return instance;
}
public loginWithParametersFrameCompletion(
parameters,
frame: Frame,
urlScheme?: string,
completion?: TnsOAuthClientLoginBlock
) {
const fullUrl = this.loginController.preLoginSetup(
frame,
urlScheme,
completion
);
this.openUrlWithParametersCompletion(fullUrl, frame);
}
public logoutWithParametersFrameCompletion(
parameters,
frame: Frame,
urlScheme?: string,
completion?: TnsOAuthClientLogoutBlock
) {
const fullUrl = this.loginController.preLogoutSetup(
frame,
urlScheme,
completion
);
this.openUrlWithParametersCompletion(fullUrl, frame);
}
private openUrlWithParametersCompletion(
fullUrl: string,
frame: Frame
): void {
this.safariViewController = SFSafariViewController.alloc().initWithURLEntersReaderIfAvailable(
NSURL.URLWithString(fullUrl),
false
);
this.safariViewController.delegate = this;
if (frame.parent) {
let topmostParent = frame.parent;
while (topmostParent.parent) {
topmostParent = topmostParent.parent;
}
topmostParent.viewController.presentViewControllerAnimatedCompletion(
this.safariViewController,
true,
null
);
} else {
frame.ios.controller.presentViewControllerAnimatedCompletion(
this.safariViewController,
true,
null
);
}
}
public resumeWithUrl(url: string): boolean {
return this.loginController.resumeWithUrl(
url,
(tokenResult: ITnsOAuthTokenResult, error) => {
if (this.safariViewController) {
this.safariViewController.dismissViewControllerAnimatedCompletion(
true,
() => {
this.loginController.completeLoginWithTokenResponseError(
tokenResult,
error
);
}
);
} else {
this.loginController.completeLoginWithTokenResponseError(
tokenResult,
error
);
}
}
);
}
// SFSafariViewControllerDelegate delegate members
public safariViewControllerDidFinish(
controller: SFSafariViewController
): void {
if (controller !== this.safariViewController) {
// Ignore this call if safari view controller doesn't match
return;
}
if (!this.loginController.authState) {
// Ignore this call if there is no pending login flow
return;
}
const er = "The login operation was canceled.";
this.loginController.completeLoginWithTokenResponseError(null, er);
}
}
return TnsOAuthLoginNativeViewController;
}
export const TnsOAuthLoginNativeViewController = setup();