Search code examples
realmrealm-mobile-platformrealm-object-server

Realm Object Server: Access Denied Error after successful login using Custom Authentication


I am trying to setup a Realm Object Server for my company's mobile application. I have to use a custom authentication to allow users to access the database.

import { BasicServer } from 'realm-object-server'
import * as path from 'path'
import { AuthProvider } from './lib/auth'

const server = new BasicServer()

server.start({
  dataPath: path.join(__dirname, '../data'),
  address: '192.168.0.24',
  authProviders: [new AuthProvider()]
 })
 .then(() => {
    console.log(`Realm Object Server was started on ${server.address}`)
 })
 .catch(err => {
    console.error(`Error starting Realm Object Server: ${err.message}`)
 })

Here is the custom auth that I have to apply. The authentication will be done by another backend server.

import { post } from 'superagent'
import { auth, User, errors } from 'realm-object-server'
import { pick } from 'lodash';

export class AuthProvider extends auth.AuthProvider {

  name = 'authprovider'

  authenticateOrCreateUser(body: any): Promise<User> {
   return post('https://XYZ/signin')
  .send({
    'email': body.user_info.email,
    'password': body.user_info.password
  })
  .then((successResponseJSON: any) => {
    return this.service.createOrUpdateUser(
      successResponseJSON.body.id,
      this.name, // this is the name of the provider,
      false, // this is if the user should or should not be an admin
      pick(successResponseJSON.body, ['id', 'email'])
    )
  })
  .catch(err => {
    throw new errors.realm.InvalidCredentials({ detail: err })
  })
 }
}

I have added code for custom authentication to the example for provided by realm to add data to the realm server. Here I am asking that the user be authenticated using 'authprovider'

var URL = "192.168.0.24:9080"

Realm.Sync.User.registerWithProvider(`http://${URL}`, {
provider: 'authprovider',
providerToken: null,
userInfo: {
  email: username,
  password: password
}
 }).then(user => {
console.log('user', user, user.identity)
Realm.open({
  sync: {
    url: `realm://${URL}/abc`,
    user: user
  },
  schema: [TickerSchema],
})

Even though the user is successfully authenticated, I am getting access denied error. I am not able to understand why.

user User {} 9ae6033cd9b55e3aca62a291af8726ea
Unhandled session token refresh error { Error: The path is invalid or current user has no access.
at new AuthError (/home/sukumar/code_snippets/realm-test/node_modules/realm/lib/errors.js:22:25)
at performFetch.then.then (/home/sukumar/code_snippets/realm-test/node_modules/realm/lib/user-methods.js:105:29)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
name: 'AuthError',
message: 'The path is invalid or current user has no access.',
stack: 'Error: The path is invalid or current user has no access.\n    at new AuthError (/home/sukumar/code_snippets/realm-test/node_modules/realm/lib/errors.js:22:25)\n    at performFetch.then.then (/home/sukumar/code_snippets/realm-test/node_modules/realm/lib/user-methods.js:105:29)\n    at <anonymous>\n    at process._tickCallback (internal/process/next_tick.js:188:7)',
type: 'https://realm.io/docs/object-server/problems/access-denied',
title: 'The path is invalid or current user has no access.',
status: 403,
code: 614 }

Solution

  • The realm url was incorrect: it should have been realm://${URL}/~/abc instead of realm://${URL}/abc