Skip to content

Commit

Permalink
Use oauth for login
Browse files Browse the repository at this point in the history
  • Loading branch information
Simona Peneva committed Feb 9, 2022
1 parent 9a3a27d commit b3abb58
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
63 changes: 39 additions & 24 deletions examples/login.html
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
<html>

<head>
<title>pCloud SDK: Examples / List folder</title>
<style>
body {
margin: 0;
padding: 20px;
}
input {
height: 28px;
line-height: 28px;
padding: 0 0 0 4px;
}
input, button {
margin: 0 0 10px 0;
}
.result {
width: 300px;
height: 50px;

#login {
display: flex;
align-items: center;
justify-content: center;
width: 185px;
height: 40px;
padding: 20px;
border-radius: 7px;
background-color: #17bed0;
border-color: transparent;
color: #fff;
cursor: pointer;
font-size: 14px;
font-family: Roboto, Arial, Tahoma, Helvetica, sans-serif !important;
box-sizing: border-box;
}
</style>
</head>

<body>

<div>
<input type="text" name="username" placeholder="Email" id="email" value="[email protected]"><br>
<input type="password" name="password" placeholder="Password" id="password" value=""><br>
<button id="login">Login</button>

<div class="result"></div>
<button id="login">Login with pCloud</button>
</div>

<script type="text/javascript" src="examples/pcloudsdk.js"></script>
<script>
'use strict';
var access_token = false;
var locationid = "";
var client = false;
el("login").addEventListener('click', function(e) {
var email = el('email').value;
var password = el('password').value;
if (email && password) {
client = pCloudSdk.createClient(null, 'pcloud');
client.login(email, password).then(console.log.bind(console));
}

el("login").addEventListener('click', function (e) {
pCloudSdk.oauth.initOauthToken({
client_id: 'p1WznE2dEPm',
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function (token, id) {
console.log(token);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);

client.login().then((metadata) => {
console.log("userinfo", metadata);
});
}
});
}, false);

function el(id) {
Expand All @@ -49,4 +63,5 @@

</script>
</body>
</html>

</html>
20 changes: 9 additions & 11 deletions src/client/methods/login.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
/* @flow */

import invariant from "invariant";
import type { MethodApi } from "../types";
import { isEmail } from "../../utils";

export default ({ client, setToken }: MethodApi) => (email: string, password: string): Promise<string> => {
invariant(typeof email === "string" && isEmail(email), "`email` must be provided.");
invariant(password, "`password` is required.");
invariant(password.length, "`password` is required.");

export default ({ client }: MethodApi) => (optionalParams?: Object): Promise<string> => {
return client
.api("userinfo", {
params: { username: email, password: password, getauth: 1, logout: 1 },
params: {
...optionalParams,
},
})
.then(response => {
return response;
})
.then(({ auth }) => {
setToken(auth);
return auth;
.catch(error => {
console.log("Error", error);
});
};

0 comments on commit b3abb58

Please sign in to comment.