Authenticated User

Pengembang dapat menggunakan fungsi pada user untuk mengidentifikasi dan mengolah pengguna terlogin.

client.user;

Dokumentasi API server:

User getters & setters

isLoggedIn

Type: getter only

Nilai yang menandakan kondisi pengguna sedang terlogin atau tidak.

Example

const isLoggedIn = client.user.isLoggedIn;

Return

boolean

firebase

Type: setter only

Menyalin objek firebase untuk keperluan internal.

Keperluan internal ini khususnya untuk secara otomatis mendapatkan dan mengirimkan token FCM (Firebase Cloud Messaging) pada method update("subscribe") dan update("unsubscribe").
Harap dicatat, penggunaan FCM ini hanya kompatibel dengan browser, tidak dengan Node.js.

Panduan di bawah ini mengasumsikan Anda menggunakan bundler (Parcel, Webpack, dsb.).

Requirements

  • Instal package firebase: npm i firebase
  • Buat file kosong dengan nama firebase-messaging-sw.js, lalu letakkan di root domain Anda (di mana aplikasi client berjalan).

Example

import { init } from "@mocobaas/client-js";
import * as firebase from "firebase/app";
import "firebase/messaging";
// Sesuaikan dengan konfigurasi aplikasi web Anda di Firebase console.
firebase.initializeApp({
apiKey: "AIzaSyBx2T3GzC1hR0a7xE4bfhUCLnkhY6-0-Bc",
authDomain: "mocobookhub.firebaseapp.com",
databaseURL: "https://mocobookhub.firebaseio.com",
projectId: "mocobookhub",
storageBucket: "mocobookhub.appspot.com",
messagingSenderId: "995737137643",
appId: "1:995737137643:web:3d1b57d8420cc94d7b29ae",
});
const client = new init("http://localhost:3000");
client.user.firebase = firebase;

User methods

get()

Minta data profil pengguna terlogin.

Return

Promise<object>

Request sukses:

{
data: {
created_at: "2020-02-05T03:28:06.184Z",
updated_at: "2020-02-05T03:28:06.184Z",
deactivated_at: null,
roles: ["Administrator", "Manager"],
email: "admin@mylib.id",
social_ids: {
google: null,
twitter: null,
facebook: null,
apple: null
},
verified: true,
fcm_tokens: [],
name: null,
country: null,
address: null,
id: "28dc27d3-8a08-4698-b24c-23e233c533a0"
}
}

Request gagal:

{
error: {
id: "1580878477547",
status: "401",
code: "NOAUTH",
title: "Authentication required"
}
}

update(scope, data)

Ubah data pengguna terlogin, menurut konteks operasi tertentu.

Arguments

nametypedescription
scopeenum
"profile", "password", "link", "unlink", "subscribe", "unsubscribe"
konteks operasi
dataobject
data.oldPasswordstring(untuk 'password')
data.newPasswordstring(untuk 'password')
data.providerenum
"facebook", "google", "twitter", "apple"
provider OAuth (untuk 'link', 'unlink')
data.oauthKeystringkey unik dari login OAuth (untuk 'link')
data.tokenstringtoken FCM (untuk 'subscribe', 'unsubscribe')

Info tambahan:

  • Akun sosial yang sudah ter-linking akan bisa digunakan untuk login.
  • Token FCM akan disimpan maksimal sejumlah konfigurasi 'firebase.maxClientsPerUser' dan dipastikan tidak ada token yang ganda dalam satu pengguna.
    Jika hendak menyimpan lagi melebihi jumlah tersebut, maka token terlama akan dihapus (prinsip FIFO).
    Token-token tersimpan menunjukkan jumlah device/browser pengguna yang akan menerima push notif.

Examples

Perbarui profil:

(async function () {
const res = await client.user.update("profile", {
name: "Osman Bostancı",
address: "Bursa",
country: "Turkey",
});
})();

Ganti password:

(async function () {
const res = await client.user.update("password", {
oldPassword: "12QWaszx",
newPassword: "QWErty55",
});
})();

Linking dengan akun Twitter. Setelah layar persetujuan OAuth:

(async function () {
const thisUrl = new URL(location.href);
// Verifikasi OAuth.
const loginRes = await client.auth.login("twitter", {
oauthToken: thisUrl.searchParams.get("oauth_token"),
oauthVerifier: thisUrl.searchParams.get("oauth_verifier"),
});
// Jika dapat error.
if (!loginRes.data) {
console.log("loginRes:", loginRes);
return;
}
const loginData = loginRes.data;
// Cantolkan akun sosial tersebut ke pengguna ini.
const linkRes = await client.user.update("link", {
provider: "twitter",
oauthKey: loginData.oauthKey,
});
})();

Lepas linking dengan akun Twitter:

(async function () {
const res = await client.user.update("unlink", {
provider: "twitter",
});
})();

Berlangganan push notif (token manual):

(async function () {
const res = await client.user.update("subscribe", {
token: "subscription-token",
});
})();

Berhenti berlangganan push notif (token manual):

(async function () {
const res = await client.user.update("unsubscribe", {
token: "subscription-token",
});
})();

Berlangganan push notif (token otomatis):

client.user.firebase = firebase;
(async function () {
const res = await client.user.update("subscribe");
})();

Berhenti berlangganan push notif (token otomatis):

client.user.firebase = firebase;
(async function () {
const res = await client.user.update("unsubscribe");
})();

Return

Promise<object>

Request sukses:

// Profil diperbarui.
{
data: {
created_at: "2020-02-05T03:28:06.184Z",
updated_at: "2020-02-05T07:37:49.379Z",
deactivated_at: null,
roles: ["Administrator", "Manager"],
email: "admin@mylib.id",
social_ids: {
google: null,
twitter: null,
facebook: null,
apple: null
},
verified: true,
fcm_tokens: [],
name: "Osman Bostancı",
country: "Turkey",
address: "Bursa",
id: "28dc27d3-8a08-4698-b24c-23e233c533a0"
},
message: "User profile updated"
}
// Token FCM disimpan.
{
data: {
created_at: "2020-02-05T03:28:06.184Z",
updated_at: "2020-02-05T07:40:02.385Z",
deactivated_at: null,
roles: ["Administrator", "Manager"],
email: "admin@mylib.id",
social_ids: {
google: null,
twitter: null,
facebook: null,
apple: null
},
verified: true,
fcm_tokens: [
"eA30wYDRECy-eTxDQ-2s4r:APA91bGwQi-j-YIicrsw1zWxP8q1iHpjLi4xP0iBmkH1vi2goDz1FsjTEhuUP4rPpE-qQUYrAgxOefhjn2Xu-sbtWoIfbwnia08uuAeFlDGaIQxFKAUhpr5Rnb_0qDb5Cnl-WQwUluoP"
],
name: "Osman Bostancı",
country: "Turkey",
address: "Bursa",
id: "28dc27d3-8a08-4698-b24c-23e233c533a0"
},
message: "Subscription token saved"
}

Request gagal:

// Harus login terlebih dahulu.
{
error: {
id: "1580889822704",
status: "401",
code: "NOAUTH",
title: "Authentication required"
}
}
// Password lama tidak cocok.
{
error: {
id: "1580889973012",
status: "404",
code: "NOTFND",
title: "'oldPassword' is not valid"
}
}
// Token untuk berhenti berlangganan tidak cocok.
{
error: {
id: "1580890042200",
status: "400",
code: "BADREQ",
title: "'token' is not valid"
}
}

logout()

Return

Request sukses:

Promise<null>

Request gagal:

Promise<object>

{
error: {
id: "1580890449380",
status: "401",
code: "NOAUTH",
title: "Authentication required"
}
}