Tôi đang cố gắng xác thực bằng Steam từ trang chủ trong Angular nhưng bất cứ khi nào tôi nhấp vào nút (có (click)
sự kiện trỏ tại login()
chức năng trong AppComponent
), thay vì được chuyển hướng đến trang Steam, hiện tại trang được làm mới và không có gì xảy ra.Xác thực hơi nước với Angular
là Mã này server-side:
'use strict';
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io').listen(server);
const jwt = require('express-jwt');
const cors = require('cors');
const passport = require('passport');
const SteamStrategy = require('passport-steam').Strategy;
const mongoose = require('mongoose');
app.use(cors());
mongoose.connect('mongodb://localhost:27017/database_test');
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
passport.use(new SteamStrategy({
returnURL: 'http://localhost:3000/auth/steam/return',
realm: 'http://localhost:3000',
apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
(identifier, profile, done) => {
process.nextTick(() => {
profile.identifier = identifier;
return done(null, profile);
});
}
));
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth/steam',
passport.authenticate('steam', { failureRedirect: 'http://localhost:4200/home' }),
(req, res) => {
res.json({"success": true, "res": res});
}
);
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: 'http://localhost:4200/home' }),
(req, res) => {
res.json({"success": true, "res": res});
}
);
server.listen(3000,() => {
console.log('Backend listening on port 3000.');
});
Đây là AuthenticationService
:
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import { JwtHelper, tokenNotExpired } from 'angular2-jwt';
import 'rxjs/add/operator/map';
@Injectable()
export class AuthenticationService {
domain = 'http://localhost:3000';
constructor(private http: Http) { }
login() {
return this.http.get(this.domain + '/auth/steam').map(res => res.json());
}
}
Đây là AppComponent
:
import { Component } from '@angular/core';
import { AuthenticationService } from './authentication.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [ AuthenticationService ]
})
export class AppComponent {
constructor(private authService: AuthenticationService) { }
login(): void {
this.authService.login().subscribe(data => {
alert(data);
});
}
}
Cả hai máy chủ đang chạy (góc và Node).
Chỉnh sửa: Tôi đã thêm nút thay vì liên kết và nhật ký cho thấy tôi có lỗi với Same-origin policy.
Chỉ muốn thêm, kudo để làm việc với Steam. Tôi chủ yếu là droning với các công cụ kinh doanh/công ty với Angular, và đánh giá lưu lượng truy cập StackOverflow, hầu hết những người khác đang có. Bạn đang đặc biệt trong việc làm một cái gì đó được, tôi giả định, liên quan đến game! Chúc may mắn cho bạn, thưa bạn, và có tất cả những niềm vui, và sau đó có một số niềm vui hơn cho phần còn lại của chúng tôi :) – Zlatko