Skip to content

Commit 0de3192

Browse files
Merge pull request #27 from NativeScript/tachev/login-event
Add login/logout events
2 parents 4f11bc4 + 337bdd0 commit 0de3192

21 files changed

+155
-50
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<StackLayout>
2-
<FacebookLoginButton [login]="onLogin"></FacebookLoginButton>
3-
<Button text="Custom Login Button" (tap)="testAction()"></Button>
4-
</StackLayout>
2+
<Label text="{{userId}}"></Label>
3+
<FacebookLoginButton (login)="onLogin($event)" (logout)="onLogout($event)"></FacebookLoginButton>
4+
<Button text="Custom Login Button" (tap)="manualLogin()"></Button>
5+
<Button text="Custom Logout Button" (tap)="manualLogout()"></Button>
6+
</StackLayout>

demo-angular/app/app.component.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
import { Component } from "@angular/core";
1+
import { Component, ChangeDetectorRef } from "@angular/core";
22
import * as Facebook from "nativescript-facebook";
33

44
@Component({
55
selector: "ns-app",
66
templateUrl: "app.component.html",
77
})
88
export class AppComponent {
9-
onLogin = function (error: string, loginResponse: Facebook.LoginResponse) {
10-
console.log("TOKEN: " + loginResponse.token);
9+
userId: string = "not logged in";
10+
11+
constructor(private ref: ChangeDetectorRef) {
12+
}
13+
14+
onLogin = function (eventData: Facebook.LoginEventData) {
15+
if (eventData.error) {
16+
alert("Error during login: " + eventData.error);
17+
} else {
18+
this.userId = "UserId: " + eventData.loginResponse.userId;
19+
this.ref.detectChanges();
20+
}
21+
};
22+
23+
manualLogin = function () {
24+
Facebook.login((error, loginResponse) => {
25+
this.userId = "UserId: " + loginResponse.userId;
26+
this.ref.detectChanges();
27+
});
1128
};
1229

13-
testAction = function () {
14-
Facebook.login((error, data) => {
15-
console.log("Success!");
30+
manualLogout = function () {
31+
Facebook.logout(() => {
32+
this.userId = "not logged in";
33+
this.ref.detectChanges();
1634
});
1735
};
36+
37+
public onLogout() {
38+
this.userId = "not logged in";
39+
this.ref.detectChanges();
40+
}
1841
}

demo/app/main-page.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
22
xmlns:Facebook="nativescript-facebook"
33
loaded="pageLoaded" class="page">
4-
<StackLayout class="p-20" style="vertical-align: bottom;" >
5-
<Label id="userIdLabel" text="{{ labelContent }}" style="padding-bottom: 100;horizontal-align: center;"/>
6-
<Facebook:LoginButton login="{{ onLogin }}"></Facebook:LoginButton>
7-
<Button tap="{{ testAction }}" text="Custom Login Button"></Button>
4+
<StackLayout>
5+
<Label text="{{ userId }}"></Label>
6+
<Facebook:LoginButton login="{{ onLogin }}" logout="{{ onLogout }}"></Facebook:LoginButton>
7+
<Button text="Log Out" tap="{{ logout }}"></Button>
88
</StackLayout>
9-
</Page>
9+
</Page>

demo/app/main-view-model.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { Observable } from 'data/observable';
2-
import { login } from "nativescript-facebook";
2+
import { LoginEventData, logout as fbLogout } from "nativescript-facebook";
33

4-
// Facebook Authentication CODE
54
export class HelloWorldModel extends Observable {
5+
public userId: string = "not logged in";
66

7-
public onLogin(error, data) {
8-
console.log("Success!");
7+
public onLogin(eventData: LoginEventData) {
8+
if (eventData.error) {
9+
alert("Error during login: " + eventData.error.message);
10+
} else {
11+
this.set("userId", "UserId: " + eventData.loginResponse.userId);
12+
}
913
}
1014

11-
public testAction() {
12-
login((error, data) => console.log("Success!"));
15+
public onLogout() {
16+
this.set("userId", "not logged in");
17+
}
18+
19+
public logout() {
20+
fbLogout(() => this.set("userId", "not logged in"));
1321
}
1422
}

src/index.android.metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./login-manager"},{"from":"./login-button"}]},{"__symbolic":"module","version":1,"metadata":{},"exports":[{"from":"./login-manager"},{"from":"./login-button"}]}]
1+
[{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./login-manager"},{"from":"./login-button"},{"from":"./login-event-data"}]},{"__symbolic":"module","version":1,"metadata":{},"exports":[{"from":"./login-manager"},{"from":"./login-button"},{"from":"./login-event-data"}]}]

src/index.android.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./login-manager";
2-
export * from "./login-button";
2+
export * from "./login-button";
3+
export * from "./login-event-data";

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./login-manager";
22
export * from "./login-button";
33
export * from "./login-response";
4+
export * from "./login-event-data";

src/index.ios.metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./login-button"},{"from":"./login-manager"}]},{"__symbolic":"module","version":1,"metadata":{},"exports":[{"from":"./login-button"},{"from":"./login-manager"}]}]
1+
[{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./login-button"},{"from":"./login-manager"},{"from":"./login-event-data"}]},{"__symbolic":"module","version":1,"metadata":{},"exports":[{"from":"./login-button"},{"from":"./login-manager"},{"from":"./login-event-data"}]}]

src/index.ios.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as applicationModule from "application";
22
export * from "./login-button";
33
export * from "./login-manager";
4+
export * from "./login-event-data";
45

56
declare class UIResponder { }
67
declare class NSDictionary { }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"__symbolic":"module","version":3,"metadata":{"LoginButtonBase":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"ui/core/view","name":"View"},"members":{"onLoginPropertyChanged":[{"__symbolic":"method"}]}},"onLoginProperty":{"__symbolic":"error","message":"Function call not supported","line":10,"character":31}}},{"__symbolic":"module","version":1,"metadata":{"LoginButtonBase":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"ui/core/view","name":"View"},"members":{"onLoginPropertyChanged":[{"__symbolic":"method"}]}},"onLoginProperty":{"__symbolic":"error","message":"Function call not supported","line":10,"character":31}}}]
1+
[{"__symbolic":"module","version":3,"metadata":{"LoginButtonBase":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"ui/core/view","name":"View"},"members":{"initNativeView":[{"__symbolic":"method"}]},"statics":{"loginEvent":"login","logoutEvent":"logout"}}}},{"__symbolic":"module","version":1,"metadata":{"LoginButtonBase":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"ui/core/view","name":"View"},"members":{"initNativeView":[{"__symbolic":"method"}]},"statics":{"loginEvent":"login","logoutEvent":"logout"}}}}]

0 commit comments

Comments
 (0)