DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

fc939d5fa30261b34470be0fcbf841d8f898fe0f — Jonni Liljamo 1 year, 9 months ago 03fb82e
feat(server, shared): send an AfterAuth payload to the client after auth
M server/src/systems/event/mod.rs => server/src/systems/event/mod.rs +26 -3
@@ 10,7 10,10 @@ use bevy_ecs::{event::EventReader, system::Res};
use bevy_log::info;
use laurelin_shared::{
    api::user::{login, register, ResponseLogin, ResponseRegister},
    server::messages::Auth,
    server::{
        channels::AfterAuthChannel,
        messages::{AfterAuth, Auth},
    },
};
use naia_bevy_server::{
    events::{AuthEvents, ConnectEvent, DisconnectEvent, ErrorEvent},


@@ 33,7 36,17 @@ pub(crate) fn auth_events(
                        register(&config.api_address, &username, &auth.email, &auth.password);
                    match wrapped.response {
                        ResponseRegister::Error(_) => server.reject_connection(&user_key),
                        ResponseRegister::Ok(_) => server.accept_connection(&user_key),
                        ResponseRegister::Ok(user) => {
                            server.accept_connection(&user_key);

                            server.send_message::<AfterAuthChannel, AfterAuth>(
                                &user_key,
                                &AfterAuth {
                                    username: user.username,
                                    cookie: wrapped.cookie,
                                },
                            )
                        }
                    }
                }
                None => {


@@ 41,7 54,17 @@ pub(crate) fn auth_events(
                    let wrapped = login(&config.api_address, &auth.email, &auth.password);
                    match wrapped.response {
                        ResponseLogin::Error(_) => server.reject_connection(&user_key),
                        ResponseLogin::Ok(_) => server.accept_connection(&user_key),
                        ResponseLogin::Ok(user) => {
                            server.accept_connection(&user_key);

                            server.send_message::<AfterAuthChannel, AfterAuth>(
                                &user_key,
                                &AfterAuth {
                                    username: user.username,
                                    cookie: wrapped.cookie,
                                },
                            )
                        }
                    }
                }
            }

M shared/src/server/channels.rs => shared/src/server/channels.rs +12 -2
@@ 6,10 6,20 @@
 * See LICENSE for licensing information.
 */

use naia_bevy_shared::{Protocol, ProtocolPlugin};
use naia_bevy_shared::{
    Channel, ChannelDirection, ChannelMode, Protocol, ProtocolPlugin, ReliableSettings,
};

#[derive(Channel)]
pub struct AfterAuthChannel;

pub struct ChannelsPlugin;

impl ProtocolPlugin for ChannelsPlugin {
    fn build(&self, protocol: &mut Protocol) {}
    fn build(&self, protocol: &mut Protocol) {
        protocol.add_channel::<AfterAuthChannel>(
            ChannelDirection::ServerToClient,
            ChannelMode::UnorderedReliable(ReliableSettings::default()),
        );
    }
}

M shared/src/server/messages/auth.rs => shared/src/server/messages/auth.rs +15 -0
@@ 25,3 25,18 @@ impl Auth {
        }
    }
}

#[derive(Message)]
pub struct AfterAuth {
    pub cookie: String,
    pub username: String,
}

impl AfterAuth {
    pub fn new(cookie: &str, username: &str) -> Self {
        Self {
            cookie: cookie.to_string(),
            username: username.to_string(),
        }
    }
}

M shared/src/server/messages/mod.rs => shared/src/server/messages/mod.rs +2 -2
@@ 9,12 9,12 @@
use naia_bevy_shared::{Protocol, ProtocolPlugin};

mod auth;
pub use auth::Auth;
pub use auth::{AfterAuth, Auth};

pub struct MessagesPlugin;

impl ProtocolPlugin for MessagesPlugin {
    fn build(&self, protocol: &mut Protocol) {
        protocol.add_message::<Auth>();
        protocol.add_message::<Auth>().add_message::<AfterAuth>();
    }
}