Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add fetch user profile into store and fix env issue #10

Merged
merged 2 commits into from Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions functions/ssrapp/index.js
Expand Up @@ -10,6 +10,13 @@ const config = {
dev: false,
buildDir: 'nuxt',
};

if ((functions.config().constant || {}).external_url) {
process.env.API_URL = functions.config().constant.external_url;
}
if ((functions.config().constant || {}).network === 'rinkeby') {
process.env.IS_TESTNET = 'TRUE';
}
const nuxt = new Nuxt(config);

const app = express();
Expand Down
30 changes: 21 additions & 9 deletions src/server/api/routes/users.js
Expand Up @@ -2,16 +2,25 @@ const axios = require('axios');
const crypto = require('crypto');
const { Router } = require('express');
const { userCollection } = require('../util/firebase');
const { getOAuthCallbackAPI, getOAuthURL } = require('../util/api');
const {
apiFetchUserProfile,
getOAuthCallbackAPI,
getOAuthURL,
} = require('../util/api');

const router = Router();

router.get('/users/self', (req, res) => {
if (req.session.user) {
res.json({ user: req.session.user });
return;
router.get('/users/self', async (req, res, next) => {
try {
if (req.session.user) {
const { data } = await apiFetchUserProfile(req);
res.json({ user: req.session.user, ...data });
return;
}
res.sendStatus(404);
} catch (err) {
next(err);
}
res.sendStatus(404);
});

router.get('/users/login', (req, res) => {
Expand All @@ -30,12 +39,14 @@ router.post('/users/login', async (req, res, next) => {
res.status(400).send('state mismatch');
}

const { data } = await axios.post(getOAuthCallbackAPI(req.body.authCode));
const { data: tokenData } = await axios.post(
getOAuthCallbackAPI(req.body.authCode)
);
const {
access_token: accessToken,
refresh_token: refreshToken,
user,
} = data;
} = tokenData;
await userCollection.doc(user).set(
{
accessToken,
Expand All @@ -46,7 +57,8 @@ router.post('/users/login', async (req, res, next) => {
req.session.user = user;
req.session.accessToken = accessToken;
req.session.state = undefined;
res.json({ user });
const { data: userData } = await apiFetchUserProfile(req);
res.json({ user, ...userData });
} catch (err) {
next(err);
}
Expand Down
7 changes: 7 additions & 0 deletions src/server/api/util/api.js
Expand Up @@ -62,6 +62,12 @@ const apiRefreshToken = refreshToken =>
axios.post(
`${LIKECOIN_API_BASE}/oauth/access_token?client_id=${LIKE_CO_CLIENT_ID}&client_secret=${LIKE_CO_CLIENT_SECRET}&grant_type=refresh_token&refresh_token=${refreshToken}`
);
const apiFetchUserProfile = req =>
sendAuthorizedRequest(req, Authorization =>
axios.get(`${LIKECOIN_API_BASE}/users/profile`, {
headers: { Authorization },
})
);
const apiFetchLikedUser = req =>
sendAuthorizedRequest(req, Authorization =>
axios.get(`${LIKECOIN_API_BASE}/like/info/liked/list`, {
Expand All @@ -81,6 +87,7 @@ const getOAuthCallbackAPI = authCode =>

module.exports = {
apiRefreshAccessToken,
apiFetchUserProfile,
apiFetchLikedUser,
apiFetchUserArticles,
getOAuthURL,
Expand Down
5 changes: 3 additions & 2 deletions src/store/index.js
Expand Up @@ -10,9 +10,10 @@ const createStore = () =>
actions: {
async nuxtServerInit({ commit }, ctx) {
try {
const { user: id } = await this.$axios.$get(api.getLoginStatus());
commit(types.USER_SET_USER_INFO, { id });
const userInfo = await this.$axios.$get(api.getLoginStatus());
commit(types.USER_SET_USER_INFO, userInfo);
} catch (err) {
console.error(err); // eslint-disable-line no-console
// no op
}
},
Expand Down
8 changes: 4 additions & 4 deletions src/store/modules/actions/user.js
Expand Up @@ -2,17 +2,17 @@ import * as types from '@/store/mutation-types';
import * as api from '@/util/api';

export async function getOAuthToken({ commit }, { authCode, state }) {
const { user } = await this.$axios.$post(api.getOAuthCallbackAPI(), {
const user = await this.$axios.$post(api.getOAuthCallbackAPI(), {
authCode,
state,
});
commit(types.USER_SET_USER_INFO, { id: user });
commit(types.USER_SET_USER_INFO, user);
}

export async function fetchLoginStatus({ commit }) {
try {
const { user } = await this.$axios.$get(api.getLoginStatus());
commit(types.USER_SET_USER_INFO, { id: user });
const user = await this.$axios.$get(api.getLoginStatus());
commit(types.USER_SET_USER_INFO, user);
return true;
} catch (err) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/store/modules/getters/user.js
@@ -1,2 +1,3 @@
export const getOAuthToken = state => state.token;
export const getUserId = state => state.user.id;
export const getUserInfo = state => state.user;
export const getUserId = state => state.user.user;