Skip to content

Instantly share code, notes, and snippets.

@kimyongin
Last active February 25, 2016 02:24
Show Gist options
  • Save kimyongin/298ec426124e617477c0 to your computer and use it in GitHub Desktop.
Save kimyongin/298ec426124e617477c0 to your computer and use it in GitHub Desktop.
var kakao_showLoginModal = function () {
return new Promise(function (resolve, reject) {
// 소셜 로그인창을 띄운다.
Kakao.Auth.login({
success: function (response) {
resolve(response);
},
fail: function (error) {
reject(error);
}
})
})
}
var kakao_queryUserInfo = function () {
return new Promise(function (resolve, reject) {
// 소셜 정보를 구한다.
Kakao.API.request({
url: '/v1/user/me',
success: function (response) {
resolve(response);
},
fail: function (error) {
reject(error);
}
})
})
}
var kakao_localLogin = function (user) {
return new Promise(function (resolve, reject) {
localLogin({ userId: user.userId }, function (response, data) {
if (response.status == 200) {
resolve(data);
} else {
reject(user);
}
});
})
}
var kakao_localSignup = function (user) {
return new Promise(function (resolve, reject) {
localSignup({ type: "kakao", userId: user.userId, userData: { userName: user.userName } }, function (response, data) {
if (response.status == 200) {
resolve(data);
} else {
reject(user);
}
});
})
}
function kakaoLogin() {
return new Promise(function (resolve, reject) {
// STEP1. 소셜 로그인창 띄우기
kakao_showLoginModal()
.then(function(response){
// STEP2. 소셜 정보 가져오기
return kakao_queryUserInfo()
// STEP3. 소셜ID로 로컬 로그인
.then(function (response) {
return kakao_localLogin({
userName : response.properties.nickname,
userId : response.id})
})
// STEP4. 로컬 로그인 결과 확인
.then(function (data) {
// 성공 --> END
return data;
},function(user){
// 실패 --> 로컬 가입
return kakao_localSignup(user);
})
.then(function(data){
// 성공 -- > END
resolve(data);
})
// ERROR
.catch(function(error){
reject(error);
})
})
// ERROR
.catch(function(error){
reject(error);
})
})
}
// ---------------------------------------
kakaoLogin().then(function (data) {
toastr.success(data, 'Welcome');
$uibModalInstance.close();
}, function (error) {
toastr.error(error, "Login Failed");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment