Skip to content
Snippets Groups Projects
Commit b679f44e authored by Clément's avatar Clément
Browse files

update: getUser() return ressource

parent 94d1519d
No related branches found
No related tags found
1 merge request!11Resolve "Replace sib-oidc with solid-auth"
......@@ -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 };
};
this.bindUserObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes') {
......@@ -139,7 +142,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);
......@@ -152,18 +155,10 @@ class SIBAuth extends HTMLElement {
* Try to replace data-src by userWebId iri on [bind-user] elements
*/
// eslint-disable-next-line class-methods-use-this
async dispatchUserInfo(id) {
id = await id;
console.log('dispath ' + id)
async dispatchUserInfo() {
const processDOM = async () => {
const elements = document.querySelectorAll('[bind-user]');
elements.forEach(element => this.applyUser(element));
/*
const elements = document.querySelectorAll(`[bind-user]:not([data-src="${id}"])`);
elements.forEach((element) => {
element.setAttribute('data-src', id);
});
*/
};
// check document state and add a hook on DOMContentLoaded if needed
if (document.readyState === 'loading') {
......@@ -190,17 +185,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);
});
});
}
......@@ -221,7 +214,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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment