Authenticated User (Identity Provider)

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

client.user;

Dokumentasi API server:

Sebelum menggunakan method-method di bawah ini, harap sesuaikan pengaturan di identity provider Anda. Saat ini MBaaS mendukung provider:

User getters & setters

isLoggedIn

Lihat: Authenticated User - isLoggedIn

firebase

Lihat: Authenticated User - firebase

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", {
useIdentityProvider: true,
});
client.user.firebase = firebase;

User methods

get(options)

Minta data profil pengguna terlogin.

Arguments

nametypedescription
optionsobject
options.include_userinfo_claimsbooleansertakan klaim-klaim dari akun identity provider di dalam response

Examples

Minta data profil, hanya lokal:

(async function () {
const res = await client.user.get();
})();

Minta data profil, termasuk userinfo_claims:

(async function () {
const res = await client.user.get({ include_userinfo_claims: true });
})();

Return

Promise<object>

Request sukses:

{
data: {
type: "IdpProfile",
id: "fc43aeb5-46fa-4feb-9f66-f8a3584c6cec",
userinfo_claims: {
sub: "fc43aeb5-46fa-4feb-9f66-f8a3584c6cec",
email: "account@somedomain.com",
email_verified: true,
name: "Doctor Grid",
client_roles: ["Member"]
},
local_profile: {
created_at: "2020-07-30T02:18:40.146Z",
updated_at: "2020-07-30T02:18:40.146Z",
deactivated_at: null,
roles: ["Reader"],
email: "account@somedomain.com",
verified: true,
fcm_tokens: [],
name: "Doctor Grid",
country: "ArcCorp",
address: "Area18"
}
}
}

Catatan: Nilai di userinfo_claims.client_roles bisa berbeda dengan local_profile.roles.

Request gagal:

// Harus login terlebih dahulu.
{
error: {
id: "1596074556755",
status: "401",
code: "NOAUTH",
title: "Authentication required"
}
}
// Profil lokal belum dibuat.
{
error: {
id: "1596074659491",
status: "404",
code: "NOTFND",
title: "Missing local profile"
}
}

create(data)

Buat profil lokal untuk pengguna terlogin.

Arguments

nametype
dataobject

Info tambahan:

  • Isi kolom email dan name akan otomatis disalin dari akun identity provider yang bersangkutan.

Examples

(async function () {
const res = await client.user.create({
address: "Area18",
country: "ArcCorp",
});
})();

Return

Promise<object>

Request sukses:

{
data: {
type: "IdpProfile",
id: "fc43aeb5-46fa-4feb-9f66-f8a3584c6cec",
local_profile: {
created_at: "2020-07-30T02:18:40.146Z",
updated_at: "2020-07-30T02:18:40.146Z",
deactivated_at: null,
roles: ["Reader"],
email: "account@somedomain.com",
verified: true,
fcm_tokens: [],
name: "Doctor Grid",
country: "ArcCorp",
address: "Area18"
}
},
message: "Local profile created"
}

Request gagal:

// Harus login terlebih dahulu.
{
error: {
id: "1596075116704",
status: "401",
code: "NOAUTH",
title: "Authentication required"
}
}
// Profil lokal sudah dibuat sebelumnya.
{
error: {
id: "1596077204598",
status: "400",
code: "BADREQ",
title: "Local profile already exists"
}
}

update(scope, data, options)

Ubah profil lokal pengguna terlogin, menurut konteks operasi tertentu.

Arguments

nametypedescription
scopeenum
"profile", "subscribe", "unsubscribe"
konteks operasi
dataobject
data.tokenstringtoken FCM (untuk 'subscribe', 'unsubscribe')
optionsobject
options.include_userinfo_claimsbooleansertakan klaim-klaim dari akun identity provider di dalam response

Info tambahan:

  • 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",
},
{ include_userinfo_claims: true }
);
})();

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: {
type: "IdpProfile",
id: "fc43aeb5-46fa-4feb-9f66-f8a3584c6cec",
userinfo_claims: {
sub: "fc43aeb5-46fa-4feb-9f66-f8a3584c6cec",
email: "account@somedomain.com",
email_verified: true,
name: "Doctor Grid",
client_roles: ["Member"]
},
local_profile: {
created_at: "2020-07-30T02:18:40.146Z",
updated_at: "2020-07-30T02:57:38.005Z",
deactivated_at: null,
roles: ["Reader"],
email: "account@somedomain.com",
verified: true,
fcm_tokens: [],
name: "Osman Bostancı",
country: "Turkey",
address: "Bursa"
}
},
message: "Local profile updated"
}
// Token FCM disimpan.
{
data: {
type: "IdpProfile",
id: "fc43aeb5-46fa-4feb-9f66-f8a3584c6cec",
local_profile: {
created_at: "2020-07-30T02:18:40.146Z",
updated_at: "2020-07-30T02:58:45.773Z",
deactivated_at: null,
roles: ["Reader"],
email: "account@somedomain.com",
verified: true,
fcm_tokens: [
"dm_yqSrVzWETuzPE2KrOOV:APA91bHboCXn1B7oh9K7Ia3CxnLN6gCMjGjDB_BjezhriPHkK9QgNINRJJhYWcrtoX_WcRJ-Z-3SUdi8sexHWJuk8ppdIAEpife_MvyL1c-yoOxjv0bgDwM_z8x_6OSsGRF2GAqR6cQx",
],
name: "Osman Bostancı",
country: "Turkey",
address: "Bursa"
}
},
message: "Subscription token saved"
}

Request gagal:

// Harus login terlebih dahulu.
{
error: {
id: "1596075012369",
status: "401",
code: "NOAUTH",
title: "Authentication required"
}
}
// Profil lokal belum dibuat.
{
error: {
id: "1596075046782",
status: "404",
code: "NOTFND",
title: "Missing local profile"
}
}
// Token untuk berhenti berlangganan tidak cocok.
{
error: {
id: "1596078347390",
status: "400",
code: "BADREQ",
title: "'token' is not valid"
}
}

logout()

Return

Request sukses:

Promise<null>

Request gagal:

Promise<object>

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