Commit 471a85ca authored by Clément's avatar Clément

update: getUser() return ressource

parent 2d9c1c45
Pipeline #3916 passed with stage
in 52 seconds
......@@ -26,17 +26,20 @@
<sib-auth>
<sib-auth-provider
data-authority="http://127.0.0.1:8000/"
data-authority="https://api.test-paris.happy-dev.fr/"
data-client_id="234528"
data-id="paris"
>
</sib-auth-provider>
<sib-auth-provider
data-authority="http://127.0.0.1:8000/"
data-authority="https://api.test-paris.happy-dev.fr/"
data-client_id="234528"
data-id="paris2"
>
</sib-auth-provider>
</sib-auth>
<script>
reset.onclick = () => {
window.sessionStorage.clear();
......@@ -47,7 +50,7 @@
</script>
<script type="module">
import { SIBBase } from 'https://unpkg.com/@startinblox/core@0.7';
import { SIBBase, store } from 'https://unpkg.com/@startinblox/core@0.7';
class TestComponent extends SIBBase {
async connectedCallback() {
......@@ -70,13 +73,17 @@
}
async update() {
const user = await this.getUser();
if (user) {
result.innerHTML = `Bonjour ${user} !`;
} else {
let user = await this.getUser();
if (!user) {
result.innerHTML = `Vous n'êtes pas connecté !`;
return;
}
user = await store.get(user);
console.log(user);
result.innerHTML = `Bonjour ${user.username} !`;
}
empty(){}
populate(){}
}
customElements.define('sib-test', TestComponent);
</script>
......
......@@ -19,24 +19,17 @@ class SIBAuthProvider extends HTMLElement {
* with the params set in the component, render view
*/
async connectedCallback() {
const { authority, id } = this.dataset; // eslint-disable-line camelcase
const { id } = this.dataset; // eslint-disable-line camelcase
this.id = id;
this.render(this.dataset);
}
/** @function
* @name disconnectedCallback
* Remove manager
*/
disconnectedCallback() {
}
/** @function
* @name login
* Start login procedure
* @param {SIBAuth} parent - SIBAuth parent instance
*/
async login(parent) {
async login() {
auth.login(this.dataset.authority);
}
......
import auth from 'https://dev.jspm.io/solid-auth-client';
import {Helpers, SIBBase} from 'https://unpkg.com/@startinblox/core@0.7';
import { Helpers, SIBBase } from 'https://unpkg.com/@startinblox/core@0.7';
const baseUrl = import.meta.url.replace(/\/[^/]*$/, '');
Helpers.importCSS(`${baseUrl}/sib-auth.css`);
......@@ -29,7 +29,7 @@ class SIBAuth extends HTMLElement {
this.install();
// this.processState();
if (await this.getUserWebId()) {
this.dispatchUserInfo(this.getUserWebId());
this.dispatchUserInfo();
}
}
......@@ -38,8 +38,7 @@ class SIBAuth extends HTMLElement {
* Clear state and user, uninstall
*/
disconnectedCallback() {
solid.auth.logout()
.then(() => alert('Goodbye!'));
auth.logout(); // .then(() => alert('Goodbye!'));
this.uninstall();
}
......@@ -51,7 +50,11 @@ class SIBAuth extends HTMLElement {
install() {
SIBBase.prototype.login = () => this.login();
SIBBase.prototype.logout = () => this.logout();
SIBBase.prototype.getUser = () => this.getUserWebId();
SIBBase.prototype.getUser = async () => {
const id = await this.getUserWebId();
if (!id) return null;
return { '@id': id };
};
}
/** @function
......@@ -116,7 +119,7 @@ class SIBAuth extends HTMLElement {
* Try to get user, the if a state is set, call the appropriate provider
*/
async processState() {
const {provider} = this.getState();
const { provider } = this.getState();
const providerElement = this.getProvider(provider);
if (providerElement) {
this.callProvider(providerElement, 'processState', this);
......@@ -129,9 +132,8 @@ class SIBAuth extends HTMLElement {
* Try to replace data-src by userWebId iri on [bind-userWebId] elements
*/
// eslint-disable-next-line class-methods-use-this
async dispatchUserInfo(id) {
id = await id;
console.log('dispath ' + id)
async dispatchUserInfo() {
const id = await this.getUserWebId();
const processDOM = async () => {
const elements = document.querySelectorAll(`[bind-user]:not([data-src="${id}"])`);
elements.forEach((element) => {
......@@ -151,17 +153,15 @@ class SIBAuth extends HTMLElement {
* Return User or undefined
* @return {User}
*/
// eslint-disable-next-line class-methods-use-this
getUserWebId() {
return new Promise((resolve, error) => {
auth.trackSession(session => {
return new Promise((resolve) => {
auth.trackSession((session) => {
if (!session) {
console.log('The user is not logged in')
return resolve(null);
} else {
console.log(`The user is ${session.webId}`)
return resolve(session.webId)
}
})
return resolve(session.webId);
});
});
}
......@@ -182,7 +182,7 @@ class SIBAuth extends HTMLElement {
* Try to logout if favorite provider is set
*/
logout() {
auth.logout().then(() => this.dispatchUserInfo())
auth.logout().then(() => this.dispatchUserInfo());
}
/** @function
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment