문서 메뉴
문서 홈
/ /
Atlas Device SDK
/ /

다중 사용자 애플리케이션 - Node.js SDK

이 페이지의 내용

  • 사용자 계정 상태
  • 장치에 새 사용자 추가
  • 장치의 모든 사용자 나열
  • 기기에서 사용자 제거
  • 활성 사용자 변경

Realm SDK를 사용하면 여러 사용자 가 특정 기기에서 앱에 동시에 로그인할 수 있습니다. Realm 클라이언트 애플리케이션은 여러 사용자가 동시에 로그인한 경우에도 단일 활성 사용자의 컨텍스트에서 실행됩니다. 다시 로그인할 필요 없이 인증된 사용자 간에 빠르게 전환할 수 있습니다.

중요

로그인한 사용자는 재인증 없이 누구나 활성 사용자가 될 수 있습니다. 이는 앱에 따라 보안 취약점일 수 있습니다. 예를 들어, 공유 장치의 사용자는 자격 증명을 제공하거나 명시적인 권한을 요구하지 않고도 동료의 로그인 계정으로 전환할 수 있습니다. 애플리케이션에 더 엄격한 인증이 필요한 경우 사용자 간 전환을 피하고 다른 사용자를 인증하기 전에 활성 사용자를 명시적으로 로그아웃하는 것이 좋습니다.

사용자가 특정 기기나 브라우저에서 Realm SDK를 통해 처음 로그인하면 SDK는 사용자 정보를 저장하고 기기에서 사용자의 상태를 추적합니다. 사용자를 적극적으로 제거하지 않는 한 사용자가 로그아웃하더라도 사용자의 데이터는 장치에 남아 있습니다.

다음 상태는 특정 시점의 기기 내 사용자를 설명합니다.

  • 인증됨: 장치에 로그인했지만 로그아웃하지 않았거나 세션이 취소되지 않은 모든 사용자입니다.

    • 활성: 현재 특정 기기에서 앱을 사용 중인 인증된 단일 사용자입니다. SDK는 이 사용자를 발신 요청과 연결하고, Atlas App Services는 데이터 액세스 권한을 평가하고 이 사용자의 컨텍스트에서 함수를 실행합니다. 자세한 내용은 활성 사용자 를 참조하세요.

    • 비활성: 현재 활성 사용자가 아닌 모든 인증된 사용자입니다. 언제든지 활성 사용자를 현재 비활성 사용자로 전환 할 수 있습니다.

  • 로그아웃: 장치에서 인증했지만 이후 로그아웃했거나 세션이 취소된 모든 사용자입니다.

다음 다이어그램은 특정 이벤트가 발생할 때 App Services 클라이언트 앱 내의 사용자가 상태 간에 전환하는 방법을 보여줍니다.

사용자가 로그아웃, 로그인 및 활성 상태, & 로그인 및 비활성 상태 등 다양한 상태에 대해 간략하게 설명하는 다이어그램입니다.

Realm SDK는 사용자가 해당 기기에 처음 로그인할 때 자동으로 해당 기기에 사용자를 추가합니다. 사용자가 로그인하면 즉시 애플리케이션의 활성 사용자가 됩니다.

예제

아래 예에서는 이메일이 joe@example.com 인 사용자가 로그인하여 활성 사용자가 됩니다. 나중에 이메일이 emma@example.com 인 사용자가 로그인하여 활성 사용자가 됩니다.

const app = new Realm.App({ id: "myapp-abcde" });
// Log in as Joe
const joeCredentials = Realm.Credentials.emailPassword("joe@example.com", "passw0rd")
const joe = await app.logIn(joeCredentials);
// The active user is now Joe
assert(joe.id === app.currentUser.id);
// Log in as Emma
const emmaCredentials = Realm.Credentials.emailPassword("emma@example.com", "pa55word")
const emma = await app.logIn(emmaCredentials);
// The active user is now Emma, but Joe is still logged in
assert(emma.id === app.currentUser.id);
const app = new Realm.App({ id: "myapp-abcde" });
// Log in as Joe
const joeCredentials = Realm.Credentials.emailPassword("joe@example.com", "passw0rd")
const joe = await app.logIn(joeCredentials);
// The active user is now Joe
assert(joe.id === app.currentUser.id);
// Log in as Emma
const emmaCredentials = Realm.Credentials.emailPassword("emma@example.com", "pa55word")
const emma = await app.logIn(emmaCredentials);
// The active user is now Emma, but Joe is still logged in
assert(emma.id === app.currentUser.id);

기기의 모든 사용자 계정 목록에 액세스할 수 있습니다. 이 목록에는 특정 기기에서 클라이언트 앱에 로그인한 모든 사용자가 포함됩니다.

예제

아래 예제에서 개발자는 Realm.App.allUsers를 반복하여 장치에 로그인한 모든 사용자를 출력합니다.

// Get a list of all Users
app.allUsers.forEach(user => {
console.log(`User with id ${user.id} is ${user.isLoggedIn ? "logged in" : "logged out"}`);
});
// Get a list of all Users
app.allUsers.forEach((user: Realm.User) => {
console.log(`User with id ${user.id} is ${user.isLoggedIn ? "logged in" : "logged out"}`);
});

장치에서 사용자에 대한 모든 정보를 제거하고 사용자를 자동으로 로그아웃할 수 있습니다.

예제

아래 예제에서는 Realm.App.removeUser() 메서드를 사용하여 현재 사용자를 장치에서 제거합니다.

// Remove the current user from the device
const user = app.currentUser;
await app.removeUser(user);
// The user is no longer the active user
if(app.currentUser) {
// The active user is now the logged in user (if there still is one) that was
// most recently active
assert(user.id !== app.currentUser.id)
}
// The user is no longer on the device
assert(app.allUsers.find(({ id }) => id === user.id) === undefined);
// Remove the current user from the device
const user = app.currentUser;
await app.removeUser(user);
// The user is no longer the active user
// The active user is now the logged in user (if there still is one) that was
// most recently active
assert(user.id !== app.currentUser?.id)
// The removed user is no longer on the device
assert(app.allUsers.find(({ id }) => id === user.id) === undefined);

언제든지 앱의 활성 사용자를 로그인한 다른 사용자로 빠르게 전환할 수 있습니다.

예제

아래 예에서는 활성 사용자가 처음에 Realm.App.switchUser() 메서드를 사용하여 user1 (으)로 전환됩니다. 나중에 활성 사용자가 user2 로 전환됩니다.

// Get some logged-in users
const authenticatedUsers = app.allUsers.filter(user => user.isLoggedIn);
const user1 = authenticatedUsers[0];
const user2 = authenticatedUsers[1];
// Switch to user1
app.switchUser(user1);
// The active user is now user1
assert(app.currentUser.id === user1.id);
// Switch to user2
app.switchUser(user2);
// The active user is now user2
assert(app.currentUser.id === user2.id);
// Get some logged-in users
const authenticatedUsers = app.allUsers.filter(user => user.isLoggedIn);
const user1 = authenticatedUsers[0];
const user2 = authenticatedUsers[1];
// Switch to user1
app.switchUser(user1);
// The active user is now user1
assert(app.currentUser.id === user1.id);
// Switch to user2
app.switchUser(user2);
// The active user is now user2
assert(app.currentUser.id === user2.id);

돌아가기

이메일/비밀번호 사용자 관리

다음

사용자 ID 연결