JaeHyeok Song

implement of sharing BOJ to kakao

This diff is collapsed. Click to expand it.
This diff could not be displayed because it is too large.
import { makeStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import React from 'react';
import path from 'path';
require("dotenv").config({
path : path.join(__dirname,'/jaksimsamil-server/.env')
});
console.log(process.env.KAKAO_JS_KEY);
const useStyles = makeStyles((theme) => ({
root: {
'& > *': {
margin: theme.spacing(1),
},
},
button: {
margin: theme.spacing(1),
},
}));
import getRecommend from "../../scripts/getRecommend";
const KakaoLoginBtn = () => {
const classes = useStyles();
const kakaoLogin = function() {
const KakaoLogin = function () {
window.Kakao.Auth.login({
success:function(auth) {
console.log(auth);
},
fail:function(err) {
console.log(err);
success: getRecommend,
fail: function (err) {
console.log("login failed", err);
}
})
}
return (
<div>
<button onClick={kakaoLogin}>KakaoLogin</button>
<button onClick={KakaoLogin}
a id="custom-login-btn" href="javascript:loginWithKakao()">
<img
src="//k.kakaocdn.net/14/dn/btqCn0WEmI3/nijroPfbpCa4at5EIsjyf0/o.jpg"
width="140"
/>
</button>
</div>
);
};
......
......@@ -9,3 +9,8 @@ export const register = ({ username, password }) =>
export const check = () => client.get('api/auth/check');
export const logout = () => client.post('/api/auth/logout');
export const kakaotoken = (kakaotoken) =>
client.post('api/auth/kakaotoken', {
kakaotoken: kakaotoken
});
......
function getRecommend(auth) {
console.log("login success", auth);
const host = 'http://localhost:23023';
const url = host+"/api/profile/recommend";
const data = { username: 'test' };
window.fetch(url, {
method: "POST",
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
.then(res => {
console.log(res);
return res.json();
})
.then(shareToKakao);
}
function shareToKakao({problem_number, problem_title, solved_date}) {
console.log('getting ready to share...');
const boj_link = "https://acmicpc.net/problem/" + problem_number.toString();
window.Kakao.Link.sendDefault({
objectType: 'text',
text: `오늘의 추천문제는 ${problem_title}`,
link: {
mobileWebUrl: boj_link,
webUrl: boj_link,
}
});
}
export {
getRecommend as default
};
\ No newline at end of file
const Router = require("koa-router");
const auth = new Router();
const authCtrl = require("./auth.ctrl");
const kakaoAuth = require("./kakaoAuth");
auth.post("/login", authCtrl.login);
auth.post("/logout", authCtrl.logout);
auth.post("/register", authCtrl.register);
auth.get("/check", authCtrl.check);
auth.post("/kakaotoken", kakaoAuth.kakaotoken);
module.exports = auth;
......
const request = require('request');
const querystring = require('querystring');
exports.kakaotoken = async (ctx) => {
const token = ctx.request.body;
'http://kapi.kakao.com/v2/api/talk/memo/default/send'
console.log(ctx);
console.log("token setted", token);
let template_objectObj = {
object_type: "text",
text: "hi", link: {
web_url: "https://developers.kakao.com",
}
};
let template_objectStr = JSON.stringify(template_objectObj);
console.log(template_objectStr);
let options = {
url: "http://kapi.kakao.com/v2/api/talk/memo/default/send",
method: "POST",
headers: {
"Authorization": "Bearer ".concat(token),
"Content-Type": "application/x-www-form-urlencoded"
}, form: {
template_object: template_objectStr
}
};
request.post(options, (err, res, body) => {
if (err != null) {
console.log(err);
}
});
};
\ No newline at end of file
......@@ -118,14 +118,16 @@ POST /api/proflie/recommend
*/
exports.recommend = async (ctx) => {
const { username } = ctx.request.body;
if (!username) {
console.log('no given username');
ctx.status = 401;
return;
}
try {
console.log('finding username:', username);
const profile = await Profile.findByUsername(username);
if (!profile) {
console.log('no profile');
ctx.status = 401;
return;
}
......@@ -137,5 +139,7 @@ exports.recommend = async (ctx) => {
//데이터가 비었을 떄 예외처리 필요
} catch (e) {
ctx.throw(500, e);
} finally {
console.log('successfully sent user recommendation')
}
};
......