Created
August 15, 2016 20:25
-
-
Save hakant/51f9fe78494f85ac0a82f1da65970ecc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Aurelia} from 'aurelia-framework'; | |
import {inject} from 'aurelia-framework'; | |
import {HttpClient} from 'aurelia-http-client'; | |
@inject(Aurelia, HttpClient) | |
export class AuthService { | |
// As soon as the AuthService is created, we query local storage to | |
// see if the login information has been stored. If so, we immediately | |
// load it into the session object on the AuthService. | |
constructor(Aurelia, HttpClient) { | |
this.http = HttpClient; | |
this.app = Aurelia; | |
this.http.configure(conf => { | |
conf.withBaseUrl("http://localhost:3000"); | |
}); | |
} | |
login() { | |
document.location = "http://localhost:3000/auth/github"; | |
} | |
logout() { | |
return this.http.createRequest('/auth/logout') | |
.withCredentials(true) | |
.asGet() | |
.send() | |
.then(response => { | |
if (response.statusCode === 200) { | |
this.app.setRoot('login'); | |
} | |
}) | |
.catch( | |
error => console.error(error) | |
); | |
} | |
isAuthenticated() { | |
return new Promise(function (resolve, reject){ | |
resolve(true); | |
}); | |
// return this.http.createRequest('/auth/account') | |
// .withCredentials(true) | |
// .asGet() | |
// .send() | |
// .then(response => { | |
// if (response.statusCode === 200) { | |
// return true; | |
// } else { | |
// return false; | |
// } | |
// }) | |
// .catch( | |
// error => { | |
// console.error(error); | |
// return false; | |
// } | |
// ); | |
} | |
get username() { | |
return this.getGitHubUser() | |
.then(user => user.username); | |
} | |
get getGitHubUser() { | |
return new Promise(function(resolve, reject){ | |
resolve({"id":"1907367","displayName":"Hakan Tuncer","username":"hakant","profileUrl":"https://github.com/hakant","emails":[{"value":"hakantuncer@gmail.com"}],"provider":"github","_raw":"{\"login\":\"hakant\",\"id\":1907367,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1907367?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/hakant\",\"html_url\":\"https://github.com/hakant\",\"followers_url\":\"https://api.github.com/users/hakant/followers\",\"following_url\":\"https://api.github.com/users/hakant/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/hakant/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/hakant/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/hakant/subscriptions\",\"organizations_url\":\"https://api.github.com/users/hakant/orgs\",\"repos_url\":\"https://api.github.com/users/hakant/repos\",\"events_url\":\"https://api.github.com/users/hakant/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/hakant/received_events\",\"type\":\"User\",\"site_admin\":false,\"name\":\"Hakan Tuncer\",\"company\":null,\"blog\":\"www.hakantuncer.com\",\"location\":\"Amsterdam\",\"email\":\"hakantuncer@gmail.com\",\"hireable\":true,\"bio\":null,\"public_repos\":12,\"public_gists\":8,\"followers\":3,\"following\":0,\"created_at\":\"2012-06-29T20:50:08Z\",\"updated_at\":\"2016-08-06T07:41:48Z\"}","_json":{"login":"hakant","id":1907367,"avatar_url":"https://avatars.githubusercontent.com/u/1907367?v=3","gravatar_id":"","url":"https://api.github.com/users/hakant","html_url":"https://github.com/hakant","followers_url":"https://api.github.com/users/hakant/followers","following_url":"https://api.github.com/users/hakant/following{/other_user}","gists_url":"https://api.github.com/users/hakant/gists{/gist_id}","starred_url":"https://api.github.com/users/hakant/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hakant/subscriptions","organizations_url":"https://api.github.com/users/hakant/orgs","repos_url":"https://api.github.com/users/hakant/repos","events_url":"https://api.github.com/users/hakant/events{/privacy}","received_events_url":"https://api.github.com/users/hakant/received_events","type":"User","site_admin":false,"name":"Hakan Tuncer","company":null,"blog":"www.hakantuncer.com","location":"Amsterdam","email":"hakantuncer@gmail.com","hireable":true,"bio":null,"public_repos":12,"public_gists":8,"followers":3,"following":0,"created_at":"2012-06-29T20:50:08Z","updated_at":"2016-08-06T07:41:48Z"}}); | |
}); | |
// return this.http.createRequest('/auth/account') | |
// .withCredentials(true) | |
// .asGet() | |
// .send() | |
// .then(response => { | |
// return response.content | |
// } | |
// ); | |
} | |
getAnotherGitHubUser(username){ | |
return this.http.createRequest(`https://api.github.com/users/${username}`) | |
.asGet() | |
.send() | |
.then(response => { | |
return response.content; | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment