Authenticating with web3 and the Bounties API

Logging in

A full guide to authenticating is available here.

The open source code we use for login is here:

import request from 'utils/request';
import { call, put, select } from 'redux-saga/effects';
import { addressSelector } from 'public-modules/Client/selectors';
import { getWeb3Client } from 'public-modules/Client/sagas';
import { actionTypes, actions } from 'public-modules/Authentication';
import { actions } from 'public-modules/Authentication';

const {
  loginSuccess,
  loginFail,
} = actions;

export function* login(action) {
  const address = yield select(addressSelector);
  const nonceEndpoint = `auth/${address}/nonce/`;
  const loginEndpoint = 'auth/login/';
  try {
    const nonceResponce = yield call(request, nonceEndpoint, 'GET');
    const nonce = nonceResponce.nonce;
    const signedUp = nonceResponce.has_signed_up;
    const { web3, proxiedWeb3 } = yield call(getWeb3Client);
    const message = web3.utils.fromUtf8(
      'Hi there! Your special nonce: ' + nonce
    );
    const signature = yield proxiedWeb3.eth.personal.sign(message, address);
    const loginOptions = {
      data: {
        public_address: address,
        signature
      }
    };
    const currentUser = yield call(
      request,
      loginEndpoint,
      'POST',
      loginOptions
    );
    yield put(loginSuccess(currentUser, signedUp));
  } catch (e) {
    yield put(loginFail(e));
  }
}

Logging out

The open source code for logout in the Bounties Network explorer is here:

import request from 'utils/request';
import { call, put, select } from 'redux-saga/effects';
import { push } from 'react-router-redux';
import { actions } from 'public-modules/Authentication';

const {
  logoutSuccess,
  logoutFail
} = actions;

export function* logout(action) {
  const endpoint = 'auth/logout/';
  try {
    yield call(request, endpoint, 'GET');
    yield put(logoutSuccess());
    yield put(push('/explorer'));
  } catch (e) {
    yield put(logoutFail(e));
  }
}
Language
Click Try It! to start a request and see the response here!