Skip to content

Commit

Permalink
feat(vue-sample): add vue sample project to demonstrate vue sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
charIeszhao committed May 23, 2022
1 parent 0c0d9e6 commit 717e53b
Show file tree
Hide file tree
Showing 17 changed files with 744 additions and 19 deletions.
15 changes: 15 additions & 0 deletions packages/vue-sample/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-env node */
require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
root: true,
extends: [
"plugin:vue/vue3-essential",
"eslint:recommended",
"@vue/eslint-config-typescript/recommended",
"@vue/eslint-config-prettier",
],
env: {
"vue/setup-compiler-macros": true,
},
};
1 change: 1 addition & 0 deletions packages/vue-sample/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
13 changes: 13 additions & 0 deletions packages/vue-sample/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logto Vue Sample</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions packages/vue-sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@logto/vue-sample",
"version": "0.1.7",
"license": "MIT",
"private": true,
"scripts": {
"preinstall": "npx only-allow pnpm",
"precommit": "lint-staged",
"start": "vite",
"check": "vue-tsc --noEmit",
"build": "pnpm check && rm -rf dist && vite build",
"lint": "eslint --ext .vue,.ts src"
},
"dependencies": {
"@logto/vue": "^0.1.7",
"vue": "^3.2.35",
"vue-router": "^4.0.14"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.1.0",
"@types/node": "^16.11.27",
"@vitejs/plugin-vue": "^2.3.1",
"@vue/eslint-config-prettier": "^7.0.0",
"@vue/eslint-config-typescript": "^10.0.0",
"@vue/tsconfig": "^0.1.3",
"eslint": "^8.5.0",
"eslint-plugin-vue": "^8.2.0",
"lint-staged": "^12.3.4",
"prettier": "^2.5.1",
"typescript": "^4.6.2",
"vite": "^2.9.5",
"vue-tsc": "^0.34.7"
}
}
Binary file added packages/vue-sample/public/favicon.ico
Binary file not shown.
22 changes: 22 additions & 0 deletions packages/vue-sample/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script setup lang="ts">
import { RouterView } from "vue-router";
</script>

<template>
<RouterView />
</template>

<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
background: #101419;
color: #dadae0;
text-align: center;
}
button {
cursor: pointer;
}
</style>
5 changes: 5 additions & 0 deletions packages/vue-sample/src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const baseUrl = window.location.origin;
export const redirectUrl = `${baseUrl}/callback`;
export const appId = "foo";

export const endpoint = "https://logto.dev"; // OIDC domain
12 changes: 12 additions & 0 deletions packages/vue-sample/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import { createLogto } from "@logto/vue";
import { appId, endpoint } from "./consts";

const app = createApp(App);

app.use(createLogto, { appId, endpoint });
app.use(router);

app.mount("#app");
27 changes: 27 additions & 0 deletions packages/vue-sample/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createRouter, createWebHistory } from "vue-router";

import CallbackView from "../views/CallbackView.vue";
import HomeView from "../views/HomeView.vue";

const router = createRouter({
history: createWebHistory(),
routes: [
{
path: "/",
name: "home",
component: HomeView,
},
{
path: "/callback",
name: "callback",
component: CallbackView,
},
{
path: "/protected-resource",
name: "protected-resource",
component: () => import("../views/ProtectedResourceView.vue"),
},
],
});

export default router;
1 change: 1 addition & 0 deletions packages/vue-sample/src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "*.vue";
16 changes: 16 additions & 0 deletions packages/vue-sample/src/views/CallbackView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts">
import { useHandleSignInCallback } from "@logto/vue";
export default {
setup() {
const { isLoading } = useHandleSignInCallback();
return {
isLoading,
};
},
};
</script>
<template>
<p v-if="isLoading">Redirecting...</p>
</template>
86 changes: 86 additions & 0 deletions packages/vue-sample/src/views/HomeView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<script lang="ts">
import { useLogto, type IdTokenClaims } from "@logto/vue";
import { RouterLink } from "vue-router";
import { ref, watchEffect } from "vue";
import { baseUrl, redirectUrl } from "../consts";
export default {
setup() {
const { isAuthenticated, getIdTokenClaims, signIn, signOut } = useLogto();
const idTokenClaims = ref<IdTokenClaims>();
const onClickSignIn = () => {
void signIn(redirectUrl);
};
const onClickSignOut = () => {
void signOut(baseUrl);
};
watchEffect(async () => {
if (isAuthenticated.value) {
const claims = getIdTokenClaims();
idTokenClaims.value = claims;
}
});
return {
idTokenClaims,
isAuthenticated,
onClickSignIn,
onClickSignOut,
};
},
components: {
RouterLink,
},
};
</script>
<template>
<div class="container">
<h3>Logto Vue Sample</h3>
<button v-if="!isAuthenticated" @click="onClickSignIn">Sign In</button>
<button v-else @click="onClickSignOut">Sign Out</button>
<div v-if="isAuthenticated && idTokenClaims">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr v-for="(value, key) in idTokenClaims" v-bind:key="key">
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
</tbody>
</table>
<RouterLink to="/protected-resource">View Protected Resource</RouterLink>
</div>
</div>
</template>
<style lang="scss">
.container {
padding: 20px;
}
.table {
margin: 50px auto;
table-layout: fixed;
width: 800px;
border: 1px solid #333;
border-spacing: 0;
th,
td {
padding: 10px;
word-wrap: break-word;
border: 1px solid #333;
}
th {
font-weight: bold;
}
}
</style>
27 changes: 27 additions & 0 deletions packages/vue-sample/src/views/ProtectedResourceView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
import { useLogto } from "@logto/vue";
import { onMounted } from "vue";
import { redirectUrl } from "../consts";
export default {
setup() {
const { isAuthenticated, isLoading, signIn } = useLogto();
onMounted(() => {
if (!isAuthenticated.value && !isLoading.value) {
void signIn(redirectUrl);
}
});
return {
isAuthenticated,
};
},
};
</script>
<template>
<p v-if="isAuthenticated">
Protected resource is only visible after sign-in.
</p>
</template>
16 changes: 16 additions & 0 deletions packages/vue-sample/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "@vue/tsconfig/tsconfig.web.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},

"references": [
{
"path": "./tsconfig.vite-config.json"
}
]
}
8 changes: 8 additions & 0 deletions packages/vue-sample/tsconfig.vite-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@vue/tsconfig/tsconfig.node.json",
"include": ["vite.config.*"],
"compilerOptions": {
"composite": true,
"types": ["node"]
}
}
22 changes: 22 additions & 0 deletions packages/vue-sample/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { fileURLToPath, URL } from "url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
optimizeDeps: {
include: ["@logto/vue"],
},
build: {
commonjsOptions: {
include: [/linked-dep/, /node_modules/],
},
},
});
Loading

0 comments on commit 717e53b

Please sign in to comment.