Skip to content

Instantly share code, notes, and snippets.

@LuckyPen3584
Created February 20, 2024 21:35
Show Gist options
  • Save LuckyPen3584/adbf9d4316476265afa1f8938508e13d to your computer and use it in GitHub Desktop.
Save LuckyPen3584/adbf9d4316476265afa1f8938508e13d to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.24+commit.e11b9ed9.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BasicAuth {
struct User {
string username;
bool exists;
}
// Mapping from user's Ethereum address to their User data
mapping(address => User) private users;
// Event to be emitted upon successful registration
event UserRegistered(address userAddress, string username);
// Event to be emitted upon successful login
event UserLoggedIn(address userAddress);
// Function to register a new user
function register(string calldata _username) external {
require(!users[msg.sender].exists, "User already exists");
users[msg.sender] = User({
username: _username,
exists: true
});
emit UserRegistered(msg.sender, _username);
}
// Function for a user to 'log in'
function login() external {
require(users[msg.sender].exists, "User does not exist");
emit UserLoggedIn(msg.sender);
}
// Function to get the current user's username
function getUsername() external view returns (string memory) {
require(users[msg.sender].exists, "User does not exist");
return users[msg.sender].username;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment