feat(auth): user login scaffold

This commit is contained in:
Dimitrie Stefanescu
2020-06-02 15:36:17 +01:00
parent 7c499f18e3
commit 71bb84dccc
8 changed files with 47 additions and 16 deletions
+2 -2
View File
@@ -70,9 +70,9 @@ exports.startHttp = async ( app ) => {
if ( process.env.NODE_ENV === 'development' ) {
debug( 'speckle:http-startup' )( 'Proxying frontend (dev mode):' )
debug( 'speckle:http-startup' )( `👉 setup application: http://localhost:${port}/setup` )
debug( 'speckle:http-startup' )( `👉 main application: http://localhost:${port}/app` )
debug( 'speckle:http-startup' )( `👉 main application: http://localhost:${port}/` )
debug( 'speckle:http-startup' )( `👉 auth application: http://localhost:${port}/auth` )
debug( 'speckle:http-startup' )( `👉 setup application: http://localhost:${port}/setup` )
debug( 'speckle:hint' )( `️ Don't forget to run "npm run dev:frontend" in a different terminal to start the vue application.` )
const frontendProxy = createProxyMiddleware( { target: 'http://localhost:8080', changeOrigin: true, ws: false, logLevel: 'silent' } )
app.use( '/', frontendProxy )
+17 -6
View File
@@ -33,12 +33,12 @@
<v-expansion-panel-header>
Requested scopes
<template v-slot:actions>
<v-icon color="primary">mdi-alert-circle</v-icon>
<v-icon color="accent">mdi-alert-circle</v-icon>
</template>
</v-expansion-panel-header>
<v-expansion-panel-content>
<ul class='my-3'>
<template v-for='scope in serverInfo.scopes'>
<template v-for='scope in serverApp.scopes'>
<li :key='scope.name'>
<b>{{scope.name}}</b>: {{scope.description}}
</li>
@@ -82,12 +82,22 @@ import debounce from 'lodash.debounce'
export default {
name: 'AppAuth',
apollo: {
profile: {
user: {
query: gql `query { user { name company } }`,
error( err ) {
console.log( 'Error retrieving profile!' )
// console.log( err )
this.loggedIn = false
},
result( { data, loading, networkStatus } ) {
if ( data.user ) {
this.loggedIn = true
} else {
this.loggedIn = false
}
console.log( data )
console.log( loading )
console.log( networkStatus )
}
},
serverInfo: {
@@ -108,16 +118,17 @@ export default {
'Google',
],
appId: null,
serverApp: { name: 'App Name', author: 'Acme Inc', firstparty: false },
serverApp: { name: 'App Name', author: 'Acme Inc', firstparty: false, scopes: [ ] },
loggedIn: null,
profile: { user: null },
user: { profile: null },
} ),
methods: {},
methods: {
},
mounted( ) {
let urlParams = new URLSearchParams( window.location.search )
this.appId = urlParams.get( 'appId' ) || 'spklwebapp'
// console.log( this.$router.history.current.query )
}
}
+10
View File
@@ -33,6 +33,16 @@ export default {
try {
let valid = this.$refs.form.validate( )
if ( !valid ) throw new Error( 'Form validation failed' )
let result = await this.$apollo.mutate( {
mutation: gql ` mutation ($login: UserLoginInput!) { userLogin( user: $login ) }`,
variables: {
login: { email: this.form.email, password: this.form.password }
}
} )
console.log( result )
let token = result.data.userLogin
onLogin( this.$apolloProvider.clients.defaultClient, token )
} catch ( err ) {
this.errorMessage = err.message
this.registrationError = true
+1 -1
View File
@@ -22,7 +22,7 @@ module.exports = {
devServer: {
historyApiFallback: {
rewrites: [
{ from: /\/app/, to: '/app.html' },
{ from: /^\/$/, to: '/app.html' },
{ from: /\/auth/, to: '/auth.html' },
{ from: /\/setup/, to: '/setup.html' }
]
+9 -6
View File
@@ -1,7 +1,7 @@
'use strict'
const root = require( 'app-root-path' )
const { ApolloError, AuthenticationError, UserInputError } = require( 'apollo-server-express' )
const { createUser, getUser, getUserRole, updateUser, deleteUser, validatePasssword } = require( '../../services/users' )
const { createUser, getUser, getUserByEmail, getUserRole, updateUser, deleteUser, validatePasssword } = require( '../../services/users' )
const { createToken, createTokenForApp, revokeToken, revokeTokenById, validateToken, getUserTokens } = require( '../../services/tokens' )
const { validateServerRole, validateScopes, authorizeResolver } = require( `${root}/modules/shared` )
const setupCheck = require( `${root}/setupcheck` )
@@ -53,11 +53,14 @@ module.exports = {
async userLogin( parent, args, context, info ) {
if ( process.env.STRATEGY_LOCAL !== 'true' )
throw new ApolloError( 'Registration method not available' )
let res = await validatePasssword( args )
let token = await createTokenForApp( { userId, appId: 'spklwebapp' } )
return token
try {
let res = await validatePasssword( args.user )
let { id: userId } = await getUserByEmail( { email: args.user.email } )
let token = await createTokenForApp( { userId, appId: 'spklwebapp' } )
return token
} catch ( err ) {
throw new Error( 'Login failed' )
}
},
async userCreate( parent, args, context, info ) {
let setupComplete = await setupCheck( )
+6
View File
@@ -45,6 +45,12 @@ module.exports = {
return user
},
async getUserByEmail( { email } ) {
let user = await Users( ).where( { email: email } ).select( '*' ).first( )
delete user.passwordDigest
return user
},
async getUserRole( id ) {
let { role } = await ServerRoles( ).where( { userId: id } ).select( 'role' ).first( )
return role
@@ -58,7 +58,8 @@ exports.up = async knex => {
const scopes = await knex( 'scopes' ).select( '*' )
const webAppScopes = scopes.filter( s => s.name !== 'server:setup' ).map( s => ( { appId: 'spklwebapp', scopeName: s.name } ) )
await knex( 'server_apps_scopes' ).insert( webAppScopes )
const mockAppScopes = [ { appId: 'mock', scopeName: 'streams:read' }, { appId: 'mock', scopeName: 'streams:write' } ]
await knex( 'server_apps_scopes' ).insert( mockAppScopes )
}