Skip to content

Commit

Permalink
Réception gitlab (Scribouilli#122)
Browse files Browse the repository at this point in the history
* fix type typo

* Réception de la connexion via gitlab

* Retour du composant AfterOauthLogin.svelte qui avait disparu dans le rebase + ajustements
  • Loading branch information
DavidBruant authored Nov 28, 2023
1 parent bfbf832 commit 36c051f
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 85 deletions.
File renamed without changes.
22 changes: 0 additions & 22 deletions assets/scripts/components/screens/AfterGithubLogin.svelte

This file was deleted.

31 changes: 31 additions & 0 deletions assets/scripts/components/screens/AfterOauthLogin.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
import Skeleton from "./../Skeleton.svelte";
import SiteCreationLoader from "./../loaders/SiteCreationLoader.svelte";
import './../../types.js'
/** @type {Promise<GithubRepository[]|Void>} */
export let currentUserReposP;
/** @type {string} */
export let type;
</script>

<Skeleton>
<section class="screen" id="loader">
{#if type === 'gitlab'}
<h2>Login depuis gitlab détecté (et on ne sait pas encore le gérer)</h2>
{:else}
{#await currentUserReposP}
<h2>Nous regardons si vous avez plusieurs sites Scribouilli…</h2>
<img
src="./assets/images/hearts.svg"
alt="cœur sur toi le temps que ça charge"
/>
{:then}
<SiteCreationLoader />
{/await}
{/if}




</section>
</Skeleton>
2 changes: 1 addition & 1 deletion assets/scripts/components/screens/ArticleContenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/** @type {Promise<EditeurFile>} */
export let fileP;
/** @type any */
/** @type {any} */
export let buildStatus;
/** @type {FileContenu[]} */
Expand Down
1 change: 1 addition & 0 deletions assets/scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const gitHubApiBaseUrl = 'https://api.github.com'
export const defaultRepoOwner = 'Scribouilli'
export const defaultThemeRepoName = 'site-template'


const body = document.querySelector('body')
if (!body) {
throw new TypeError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@

import page from 'page'

import { svelteTarget, defaultRepositoryName } from '../config'
import {
svelteTarget,
defaultRepositoryName,
ACCESS_TOKEN_STORAGE_KEY,
OAUTH_PROVIDER_STORAGE_KEY,
TOCTOCTOC_ACCESS_TOKEN_URL_PARAMETER,
TOCTOCTOC_OAUTH_PROVIDER_URL_PARAMETER,
} from '../config'
import { replaceComponent } from '../routeComponentLifeCycle'
import store from '../store'
import AfterGithubLogin from '../components/screens/AfterGithubLogin.svelte'
import AfterOauthLogin from '../components/screens/AfterOauthLogin.svelte'
import {
fetchCurrentUserRepositories,
createRepositoryForCurrentAccount,
} from '../actions.js'
import {
ACCESS_TOKEN_STORAGE_KEY,
OAUTH_PROVIDER_STORAGE_KEY,
TOCTOCTOC_ACCESS_TOKEN_URL_PARAMETER,
TOCTOCTOC_OAUTH_PROVIDER_URL_PARAMETER,
} from './../config.js'

const storeOAuthProviderAccess = () => {
const url = new URL(location.href)

console.log(
'type',
url.searchParams.get(TOCTOCTOC_OAUTH_PROVIDER_URL_PARAMETER),
)

if (
url.searchParams.has(TOCTOCTOC_ACCESS_TOKEN_URL_PARAMETER) &&
url.searchParams.get(TOCTOCTOC_ACCESS_TOKEN_URL_PARAMETER) !== null &&
Expand All @@ -32,7 +38,6 @@ const storeOAuthProviderAccess = () => {
const providerName = url.searchParams.get(
TOCTOCTOC_OAUTH_PROVIDER_URL_PARAMETER,
)
history.replaceState(undefined, '', url)

// @ts-ignore
localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, accessToken)
Expand All @@ -46,29 +51,51 @@ const storeOAuthProviderAccess = () => {
}
}

export default () => {
/**
* @param {import('page').Context} _
*/
export default ({ querystring }) => {
storeOAuthProviderAccess()

const currentUserReposP = fetchCurrentUserRepositories().then(repos => {
if (repos.length === 0) {
// If the user has no repository, we automatically create one for them.
createRepositoryForCurrentAccount(defaultRepositoryName)
} else {
store.mutations.setReposForAccount({
login: store.state.login,
repos,
})
const type = store.state.oAuthProvider?.name

let currentUserReposP

// no type is implicitly github for historical reasons (which will certainly be irrelevant in, say, 2025)
if (!type || type === 'github') {
currentUserReposP = fetchCurrentUserRepositories().then(repos => {
if (repos.length === 0) {
// If the user has no repository, we automatically create one for them.
createRepositoryForCurrentAccount(defaultRepositoryName)
} else {
store.mutations.setReposForAccount({
login: store.state.login,
repos,
})

page.redirect('/selectionner-un-site')
page.redirect('/selectionner-un-site')
}
})
} else {
currentUserReposP = Promise.resolve()
if (type === 'gitlab') {
console.log(
'Connexion depuis gitlab détectée et on ne sait rien en faire',
)
} else {
console.error(`Type dans l'URL non reconnu:`, type)
}
})
}

const afterGithubLogin = new AfterGithubLogin({
const afterOauthLogin = new AfterOauthLogin({
target: svelteTarget,
props: {
// @ts-ignore
currentUserReposP,
// @ts-ignore
type,
},
})

replaceComponent(afterGithubLogin, () => {})
replaceComponent(afterOauthLogin, () => {})
}
2 changes: 1 addition & 1 deletion assets/scripts/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Login from '../components/screens/Login.svelte'

export default () => {
const destination =
location.origin + store.state.basePath + '/after-github-login'
location.origin + store.state.basePath + '/after-oauth-login'
const client_id = '64ecce0b01397c2499a6'
const redirect_url = 'https://toctoctoc.lechappeebelle.team/github-callback'
const githubLoginHref = `https://github.com/login/oauth/authorize?client_id=${client_id}&scope=public_repo,user:email&redirect_uri=${redirect_url}?destination=${destination}`
Expand Down
82 changes: 47 additions & 35 deletions assets/scripts/routes/main.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
//@ts-check

import page from "page";

import store from "../store.js";

import welcome from "./welcome.js";
import account from './account.js';
import login from './login.js';
import afterGithubLogin from "./after-github-login.js";
import atelierListArticles from "./atelier-list-articles.js";
import atelierListPages from "./atelier-list-pages.js";
import atelierPages from "./atelier-pages.js";
import atelierArticles from "./atelier-articles.js";
import createGithubAccount from "./create-github-account.js";
import selectOrCreateSite from "./select-or-create-site.js";
import createNewSite from "./create-new-site.js";
import startFromExistingSite from "./start-from-existing-site.js";
import settings from "./settings.js";

page("/", welcome);
page("/account", account);
page("/login", login);
page("/after-github-login", afterGithubLogin)
page("/atelier-list-articles", atelierListArticles)
page("/atelier-list-pages", atelierListPages)
page("/atelier-page", atelierPages)
page("/atelier-article", atelierArticles)
page("/create-github-account", createGithubAccount)
page("/selectionner-un-site", selectOrCreateSite)
page("/creer-un-nouveau-site", createNewSite)
page("/partir-dun-site-existant", startFromExistingSite)
page("/settings", settings)

page.base(store.state.basePath);

page.start();
import page from 'page'

import store from '../store.js'

import welcome from './welcome.js'
import account from './account.js'
import login from './login.js'
import afterOauthLogin from './after-oauth-login.js'
import atelierListArticles from './atelier-list-articles.js'
import atelierListPages from './atelier-list-pages.js'
import atelierPages from './atelier-pages.js'
import atelierArticles from './atelier-articles.js'
import createGithubAccount from './create-github-account.js'
import selectOrCreateSite from './select-or-create-site.js'
import createNewSite from './create-new-site.js'
import startFromExistingSite from './start-from-existing-site.js'
import settings from './settings.js'

page('/', welcome)
page('/account', account)
page('/login', login)

page(
'/after-github-login',
(_, next) => {
console.warn(
`Utilisation de la route dépréciée '/after-github-login'. Utiliser plutôt '/after-oauth-login'`,
)
next()
},
afterOauthLogin,
)
page('/after-oauth-login', afterOauthLogin)

page('/atelier-list-articles', atelierListArticles)
page('/atelier-list-pages', atelierListPages)
page('/atelier-page', atelierPages)
page('/atelier-article', atelierArticles)
page('/create-github-account', createGithubAccount)
page('/selectionner-un-site', selectOrCreateSite)
page('/creer-un-nouveau-site', createNewSite)
page('/partir-dun-site-existant', startFromExistingSite)
page('/settings', settings)

page.base(store.state.basePath)

page.start()
2 changes: 0 additions & 2 deletions assets/scripts/scripts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ts-check

import page from 'page'
import './routes/main.js'

// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"homepage": "https://github.com/daktary-team/scribouilli#readme",
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
"**/*.js": "prettier --write --ignore-unknown"
},
"type": "module",
"devDependencies": {
Expand Down

0 comments on commit 36c051f

Please sign in to comment.