Skip to content

Commit

Permalink
Small fix in chat window and authentication flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemvik committed Aug 23, 2019
1 parent 6812b85 commit 81e2ef4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
35 changes: 28 additions & 7 deletions source/chat-service/ClientApp/src/components/ChatRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import {HubConnectionBuilder} from '@aspnet/signalr';
export class ChatRoom extends Component {
constructor(props) {
super(props);
this.state = {messages: [], input: "", connected: false};
this.state = {members: [], messages: [], input: "", connected: false};
this.alias = props.location.state.alias;

let token = props.location.state.token;

console.log(this.alias);
console.log(token);

this.connection = new HubConnectionBuilder()
.withUrl("/chat", {accessTokenFactory: () => token})
Expand All @@ -23,8 +20,10 @@ export class ChatRoom extends Component {
}.bind(this)).catch(function (error) {
return console.error(error.toString());
});

this.connection.on("ReceiveMessage", this.addMessage.bind(this));
this.connection.on("UserConnected", this.addChatUser.bind(this));
this.connection.on("UserDisconnected", this.removeChatUser.bind(this));
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
Expand All @@ -35,12 +34,24 @@ export class ChatRoom extends Component {
})
}

addChatUser(user) {
this.setState((state) => {
return {members: [...state.members, {user: user}]};
});
}

removeChatUser(user) {
this.setState((state) => {
return {members: state.members.filter(m => m.user !== user)};
});
}

handleChange(event) {
this.setState({input: event.target.value})
}

handleSubmit(event) {
this.connection.invoke("SendMessage", this.props.alias, this.state.input).catch(err => console.error(err));
this.connection.invoke("SendMessage", this.alias, this.state.input).catch(err => console.error(err));
event.preventDefault();
}

Expand All @@ -49,11 +60,17 @@ export class ChatRoom extends Component {
return (<p><em>Connecting...</em></p>);
}

let users = this.state.members.map(mem => <li key={mem.user}><span>{mem.user}</span></li>);

console.dir(this.state.messages);
let contents = this.state.messages.map(msg => <li key={msg.message}>
<span>{msg.user}</span>: <span>{msg.message}</span></li>);

return (
<div>
<ul>
{users}
</ul>
<ul>
{contents}
</ul>
Expand All @@ -67,4 +84,8 @@ export class ChatRoom extends Component {
</div>
);
}

componentWillUnmount() {
this.connection.stop().catch(err => console.error(err));
}
}
2 changes: 1 addition & 1 deletion source/chat-service/ClientApp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Login extends Component {
'Password': this.state.password
})
}).then(response => response.json()).then(response => {
this.setState({token: response.Token, alias: response.Alias, authorized: true})
this.setState({token: response.token, alias: response.alias, authorized: true})
});

event.preventDefault();
Expand Down
13 changes: 13 additions & 0 deletions source/chat-service/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
Expand All @@ -11,5 +12,17 @@ public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}

public override async Task OnConnectedAsync()
{
await Clients.All.SendAsync("UserConnected", Context.User.Identity.Name);
await base.OnConnectedAsync();
}

public override async Task OnDisconnectedAsync(Exception exception)
{
await Clients.All.SendAsync("UserDisconnected", Context.User.Identity.Name);
await base.OnDisconnectedAsync(exception);
}
}
}
5 changes: 3 additions & 2 deletions source/chat-service/Services/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public AuthService(ChatDbContext chatDbContext, IOptions<SecuritySettings> secur

public async Task<(ChatUser User, string Token)> Authenticate(string userName, string password)
{
var userAuth = await ChatDbContext.Auths.FirstOrDefaultAsync(auth => auth.Login == userName
&& auth.Password == password);
var userAuth = await ChatDbContext.Auths.Include(auth => auth.User)
.FirstOrDefaultAsync(auth => auth.Login == userName
&& auth.Password == password);

if (userAuth == null)
{
Expand Down

0 comments on commit 81e2ef4

Please sign in to comment.