mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-20 03:42:10 +03:00
generating and saving accessToken using hsCode maker for regular login
This commit is contained in:
parent
d33df18e04
commit
2e57286cfb
@ -1,6 +1,8 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import Config from 'react-native-config';
|
import Config from 'react-native-config';
|
||||||
import VersionNumber from 'react-native-version-number';
|
import VersionNumber from 'react-native-version-number';
|
||||||
|
import {store} from '../redux/store/store';
|
||||||
|
import {get} from 'lodash';
|
||||||
|
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
baseURL: Config.ECENCY_BACKEND_API,
|
baseURL: Config.ECENCY_BACKEND_API,
|
||||||
@ -12,6 +14,19 @@ const api = axios.create({
|
|||||||
|
|
||||||
api.interceptors.request.use((request) => {
|
api.interceptors.request.use((request) => {
|
||||||
console.log('Starting ecency Request', 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;
|
return request;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ import { getSCAccessToken } from '../ecency/ecency';
|
|||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import AUTH_TYPE from '../../constants/authType';
|
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) => {
|
export const login = async (username, password, isPinCodeOpen) => {
|
||||||
let loginFlag = false;
|
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;
|
let jsonMetadata;
|
||||||
try {
|
try {
|
||||||
jsonMetadata = JSON.parse(account.posting_json_metadata) || '';
|
jsonMetadata = JSON.parse(account.posting_json_metadata) || '';
|
||||||
@ -101,6 +119,7 @@ export const login = async (username, password, isPinCodeOpen) => {
|
|||||||
currentUsername: username,
|
currentUsername: username,
|
||||||
};
|
};
|
||||||
await setAuthStatus(authData);
|
await setAuthStatus(authData);
|
||||||
|
await setSCAccount(scTokens);
|
||||||
|
|
||||||
// Save user data to Realm DB
|
// Save user data to Realm DB
|
||||||
await setUserData(account.local);
|
await setUserData(account.local);
|
||||||
@ -108,6 +127,7 @@ export const login = async (username, password, isPinCodeOpen) => {
|
|||||||
return {
|
return {
|
||||||
...account,
|
...account,
|
||||||
password,
|
password,
|
||||||
|
accessToken:get(scTokens, 'access_token', '')
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return Promise.reject(new Error('auth.invalid_credentials'));
|
return Promise.reject(new Error('auth.invalid_credentials'));
|
||||||
|
Loading…
Reference in New Issue
Block a user