【问题标题】:Issues Importing Auth0 into Angular2/.NETCore Project将 Auth0 导入 Angular 2/.NET Core 项目的问题
【发布时间】:2017-09-03 04:43:43
【问题描述】:

我正在尝试实现此自定义登录 auth0 service,但我在导入时遇到了问题。我的麻烦在于declare var auth0: any。每当我尝试得到:

EXCEPTION: Uncaught (in promise): ReferenceError: auth0 is not defined

问题是,我尝试使用Login example 使用declare var Auth0Lock: any 来执行此操作,并且成功了。我正在使用与 webpack 一起编译的 .NET Core+Angular2 解决方案。我不确定是否必须在我的webpack.config.js 中添加一些内容,但这里是:

// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
resolve: { extensions: [ '', '.js', '.ts', '.scss' ] },
output: {
    filename: '[name].js',
    publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
    loaders: [
        { test: /\.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
        { test: /\.ts$/, include: /ClientApp/, loader: 'angular2-router-loader' },
        { test: /\.html$/, loader: 'raw' },
        { test: /\.css$/, loader: 'to-string!css' },
        { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } },
        { test: /\.scss$/, exclude: /node_modules/, loaders: ["raw-loader", "sass-loader"] },
        { test: /jquery\.flot\.resize\.js$/, loader: "imports?this=>window" }
    ]
}
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
entry: {
    'main-client': './ClientApp/boot-client.ts'
},
output: { path: path.join(__dirname, './wwwroot/dist') },
devtool: isDevBuild ? 'inline-source-map' : null,
plugins: [
    new webpack.DllReferencePlugin({
        context: __dirname,
        manifest: require('./wwwroot/dist/vendor-manifest.json')
    })
].concat(isDevBuild ? [] : [
    // Plugins that apply in production builds only
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin()
])
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
entry: { 'main-server': './ClientApp/boot-server.ts' },
output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map',
externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // 
Don't bundle .js files from node_modules
});

//var authBundleConfig = merge(sharedConfig, {
//    entry: "../../node_modules/auth0-js/src/index.js",
//    output: {
//        filename: filename.join(__dirname, 'build.js')
//    },
//    module: {
//        loaders: [{
//            test: /\.js$/,
//            exclude: /(node_modules|bower_components)/,
//            loader: 'babel'
//        }]
//    }
//});

module.exports = [clientBundleConfig, serverBundleConfig/*, authBundleConfig*/];

如您所见,我尝试添加 authBundle,但没有成功。

我的服务:

// app/core/authentication/auth.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { tokenNotExpired } from 'angular2-jwt';

import { myConfig } from './auth.config';

const auth0 = require('auth0-js').default;
//declare var auth0: any;

@Injectable()
export class AuthService {
// Configure Auth0
private auth0 = new auth0.WebAuth({
    domain: myConfig.domain,
    clientID: myConfig.clientID,
    redirectUri: myConfig.callbackURL,
    responseType: 'token id_token'
});

constructor(private router: Router) {
}
...

任何帮助表示赞赏。

【问题讨论】:

    标签: angular authentication typescript asp.net-core auth0


    【解决方案1】:

    我通过 npm 安装最新的 auth0-js typings 并在我的 auth.service 中使用 import * as auth0 from 'auth0-js'; 使导入工作。

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多