generating and saving accessToken using hsCode maker for regular login

This commit is contained in:
Nouman Tahir 2021-06-23 17:51:12 +05:00
parent d33df18e04
commit 2e57286cfb
2 changed files with 35 additions and 0 deletions

View File

@ -1,6 +1,8 @@
import axios from 'axios';
import Config from 'react-native-config';
import VersionNumber from 'react-native-version-number';
import {store} from '../redux/store/store';
import {get} from 'lodash';
const api = axios.create({
baseURL: Config.ECENCY_BACKEND_API,
@ -12,6 +14,19 @@ const api = axios.create({
api.interceptors.request.use((request) => {
console.log('Starting ecency Request', request);
const state = store.getState();
const accessToken = get(state, 'account.currentAccount.accessToken');
if(accessToken){
if(!request.data){
request.data = {}
}
request.data.code = accessToken;
console.log('Added access token:', accessToken)
}else{
console.warn("No access token available")
}
return request;
});

View File

@ -22,6 +22,8 @@ import { getSCAccessToken } from '../ecency/ecency';
// Constants
import AUTH_TYPE from '../../constants/authType';
import { cryptoUtils } from '@hiveio/dhive';
import { makeHsCode } from '../../utils/hive-signer-helper';
export const login = async (username, password, isPinCodeOpen) => {
let loginFlag = false;
@ -62,6 +64,22 @@ export const login = async (username, password, isPinCodeOpen) => {
}
});
// Prepare hivesigner code
const signer = (message) => {
const hash = cryptoUtils.sha256(message)
return new Promise(
(resolve) => {
const key = privateKeys['activeKey']
const signedKey = key.sign(hash)
const signedStr = signedKey.toString();
resolve(signedStr)
}
);
}
const code = await makeHsCode(account.name, signer);
const scTokens = await getSCAccessToken(code);
let jsonMetadata;
try {
jsonMetadata = JSON.parse(account.posting_json_metadata) || '';
@ -101,6 +119,7 @@ export const login = async (username, password, isPinCodeOpen) => {
currentUsername: username,
};
await setAuthStatus(authData);
await setSCAccount(scTokens);
// Save user data to Realm DB
await setUserData(account.local);
@ -108,6 +127,7 @@ export const login = async (username, password, isPinCodeOpen) => {
return {
...account,
password,
accessToken:get(scTokens, 'access_token', '')
};
}
return Promise.reject(new Error('auth.invalid_credentials'));