Coder Social home page Coder Social logo

jozo233 / authorizer-vue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from authorizerdev/authorizer-vue

0.0 0.0 0.0 404 KB

Vue SDK for [authorizer.dev](https://authorizer.dev/)

License: MIT License

Shell 0.15% TypeScript 13.80% CSS 4.07% HTML 0.49% Vue 81.48%

authorizer-vue's Introduction

authorizer-vue

Authorizer Vue SDK allows you to implement authentication in your Vue application quickly. It also allows you to access the user profile.

Here is a quick guide on getting started with @authorizerdev/authorizer-vue package.

Step 1 - Create Instance

Get Authorizer URL by instantiating Authorizer instance and configuring it with necessary environment variables.

Step 2 - Install package

Install @authorizerdev/authorizer-vue library

npm i --save @authorizerdev/authorizer-vue
OR
yarn add @authorizerdev/authorizer-vue

Step 3 - Configure Provider and use Authorizer Components

Authorizer comes with a Provider component that exposes a composable function to return a reactive context to it's children by using the useAuthorizer injection key, internally toRefs are used when returning the reactive state so that the consuming component(s) can destructure/spread the returned object without losing reactivity and each property could be watched to perform actions accordingly.

<template>
	<div :style="{ display: 'flex', justifyContent: 'center' }">
		<authorizer-provider
			:config="{
				authorizerURL: 'http://localhost:8080',
				redirectURL: window.location.origin,
				clientID: 'AUTHORIZER_CLIENT_ID'
			}"
			:onStateChangeCallback="stateChangeCallback"
		>
			<router-view />
		</authorizer-provider>
	</div>
</template>

<script lang="ts">
import { AuthorizerProvider } from '@authorizerdev/authorizer-vue';
import type { AuthorizerState } from '@authorizerdev/authorizer-vue/dist/types/types';

export default {
	components: {
		'authorizer-provider': AuthorizerProvider
	},
	setup() {
		const stateChangeCallback = (state: AuthorizerState) => {
			console.log('state changed ==>> ', state);
		};
		return {
			stateChangeCallback,
			window
		};
	}
};
</script>
<template>
	<div>
		<h1 :style="{ textAlign: 'center' }">Welcome to Authorizer</h1>
		<br />
		<authorizer-root :onLogin="onLogin" />
	</div>
</template>

<script lang="ts">
import { inject, watch } from 'vue';
import { useRouter } from 'vue-router';
import { AuthorizerRoot } from '@authorizerdev/authorizer-vue';
import type { AuthorizerContextOutputType } from '@authorizerdev/authorizer-vue/dist/types/types';

export default {
	name: 'Login',
	components: {
		'authorizer-root': AuthorizerRoot
	},
	setup() {
		const useAuthorizer = inject('useAuthorizer') as () => AuthorizerContextOutputType;
		const { token, config } = useAuthorizer?.();
		const router = useRouter();
		const onLogin = () => {
			console.log('test login');
		};
		watch(
			token,
			(newvalue) => {
				if (newvalue) {
					console.log('access token ==>> ', token?.value?.access_token);
					router.push('/dashboard');
				}
			},
			{
				immediate: true
			}
		);
		config &&
			watch(config.is_basic_authentication_enabled, (newvalue, oldvalue) => {
				console.log('basic auth enabled (old value) ==>> ', oldvalue);
				console.log('basic auth enabled (new value) ==>> ', newvalue);
			});
		return {
			onLogin
		};
	}
};
</script>

Commands

Local Development

The recommended workflow is to run authorizer in one terminal:

npm run dev # or yarn dev

This starts a local dev-server with a sandbox environment.

npm run build # or yarn build

This uses Vite to build the project files to /dist and call build:types script.

npm run build:types # or yarn build:types

This generates TypeScript declaration files for our .vue files (using vue-tsc).

npm run typecheck # or yarn typecheck

This runs a typecheck against our Vue components to make sure there are no type errors (using vue-tsc).

Configuration

Typescript:

  • Root tsconfig: tsconfig.json contains a reference to all the others tsconfig files.
  • Components tsconfig: tsconfig.app.json will take care of compiling our Vue components inside src/.
  • Build-types tsconfig: tsconfig.build-types.json will take care of generating the proper types declaration files of our Vue components inside src/.
  • Tools tsconfig: tsconfig.config.json will take care of all our tooling configuration files (we only have vite at the moment).

Vite:

  • Vite requires a configuration to compile and bundle .vue to .js files that can be consumed through an npm module. It uses rollup.js under the hood, check out the comments in vite.config.ts file in the project root to learn more about the configuarition details.

Eslint:

  • All required linting configurations are specified in the .elsintrc.json file in the project root, check the comments in each section to learn more about the configuarition details.

Prettier:

  • We have the "usePrettierrc" option set to true in the eslint configuration file which tells the prettier-vue plugin to use the Prettier configuration file .prettierrc in the project root directory and override any default settings.

Husky:

  • A pre-commit hook is set in .husky/pre-commit which formats the code and checks for any linting errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.