Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (55 sloc) 2.57 KB
import { BasicServer, auth, errors, User } from 'realm-object-server'
import * as Realm from 'realm'
import * as path from 'path'
import { login } from './fake-company-login' // this is just some fake
const server = new BasicServer()
class MyCustomAuth extends auth.AuthProvider {
name = "mycustomauth"
async authenticateOrCreateUser(body: any): Promise<User> {
try {
const user_info = body.user_info
const userId: string = user_info.userId
const pin: string =
const departmentId: string = user_info.departmentId
const response = await login(userId, pin, departmentId)
return this.service.createOrUpdateUser(
response.userId, // this is your company's way of identifying your userId, // this should always be set to
response.isAdmin, { // add some meta data to this user
departmentId: response.departmentId,
} catch (err) {
throw new errors.realm.InvalidCredentials({ detail: err })
const start = async () => {
try {
await server.start({
// This is the location where ROS will store its runtime data
dataPath: path.join(__dirname, '../data'),
// register the auth provider
authProviders: [new MyCustomAuth()],
featureToken: "<YOUR-FEATURE-TOKEN>",
console.log(`Realm Object Server was started on ${server.address}`)
// the server has started now! let's post some json data up
// this is some custom payload you can send to the Realm Object Server backend
// in the `authenticateOrCreateUser` you'll find this data mapped to body.user_info
const customLoginPayload = {
userId: 'joe',
pin: '1234',
departmentId: 'sales'
const credentials = Realm.Sync.Credentials.custom(
'mycustomauth', // make sure this matches your so that the request is mapped to the correct authentication provider
'', // you don't have to worry about this.
customLoginPayload) // this is your custom payload
const user = await Realm.Sync.User.login(`http://${server.address}`, credentials);
console.log(`Congratulations! You've logged in successfully! You Realm Sync UserId is ${user.identity}`)
} catch (err) {
console.error(`Error starting Realm Object Server: ${err.message}`)