Authkey is now stored in .config file to allow automatic logging in
This commit is contained in:
@@ -4,6 +4,7 @@ import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.api.client.ApiClient;
|
||||
import eu.crushedpixel.replaymod.api.client.ApiException;
|
||||
import eu.crushedpixel.replaymod.api.client.holders.AuthKey;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -32,12 +33,33 @@ public class AuthenticationHandler {
|
||||
public static void register(String username, String mail, String password) throws IOException, ApiException {
|
||||
AuthKey auth = apiClient.register(username, mail, password);
|
||||
authkey = auth.getAuthkey();
|
||||
saveAuthkey(authkey);
|
||||
}
|
||||
|
||||
public static boolean loadAuthkeyFromConfig() {
|
||||
Property p = ReplayMod.instance.config.get("authkey", "authkey", "null");
|
||||
|
||||
String key = null;
|
||||
if(!(p.getString().equals("null"))) {
|
||||
key = p.getString();
|
||||
}
|
||||
|
||||
if(key != null) {
|
||||
boolean succ = apiClient.checkAuthkey(key);
|
||||
if(succ) {
|
||||
authkey = key;
|
||||
} else {
|
||||
saveAuthkey("null");
|
||||
}
|
||||
return succ;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int authenticate(String username, String password) {
|
||||
try {
|
||||
authkey = ReplayMod.apiClient.getLogin(username, password).getAuthkey();
|
||||
saveAuthkey(authkey);
|
||||
return SUCCESS;
|
||||
} catch(ApiException e) {
|
||||
return INVALID;
|
||||
@@ -50,6 +72,7 @@ public class AuthenticationHandler {
|
||||
try {
|
||||
ReplayMod.apiClient.logout(authkey);
|
||||
authkey = null;
|
||||
saveAuthkey("null");
|
||||
return SUCCESS;
|
||||
} catch(ApiException e) {
|
||||
return INVALID;
|
||||
@@ -57,4 +80,10 @@ public class AuthenticationHandler {
|
||||
return NO_CONNECTION;
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveAuthkey(String authkey) {
|
||||
ReplayMod.instance.config.removeCategory(ReplayMod.config.getCategory("authkey"));
|
||||
ReplayMod.instance.config.get("authkey", "authkey", authkey);
|
||||
ReplayMod.instance.config.save();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user