Skip to content

Commit 61b59a2

Browse files
Add IAuthenticationModule
1 parent 30c7c81 commit 61b59a2

16 files changed

+254
-0
lines changed

Packages/UGF.Module.Authentication/Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UGF.Application.Runtime;
2+
3+
namespace UGF.Module.Authentication.Runtime
4+
{
5+
public abstract class AuthenticationModule<TDescription> : ApplicationModuleAsync<TDescription>, IAuthenticationModule where TDescription : class, IApplicationModuleDescription
6+
{
7+
public bool IsSigned { get { return OnIsSigned(); } }
8+
9+
protected AuthenticationModule(TDescription description, IApplication application) : base(description, application)
10+
{
11+
}
12+
13+
public void SignOut()
14+
{
15+
OnSignOut();
16+
}
17+
18+
protected abstract bool OnIsSigned();
19+
protected abstract void OnSignOut();
20+
}
21+
}

Packages/UGF.Module.Authentication/Runtime/AuthenticationModule.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using UGF.Application.Runtime;
2+
3+
namespace UGF.Module.Authentication.Runtime
4+
{
5+
public interface IAuthenticationModule : IApplicationModule
6+
{
7+
bool IsSigned { get; }
8+
9+
void SignOut();
10+
}
11+
}

Packages/UGF.Module.Authentication/Runtime/IAuthenticationModule.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "UGF.Module.Authentication.Runtime",
3+
"rootNamespace": "UGF.Module.Authentication.Runtime",
4+
"references": [
5+
"GUID:a4312e46e9f64bb4aa011ac96ffd2a61",
6+
"GUID:6c7389a7d4bbed54e96eb1e71a69798e",
7+
"GUID:3ad3a680cb737c6428dc532d551e7bb7",
8+
"GUID:088d00b6871540e44bce58af1a3f0f17",
9+
"GUID:d067af32d09350a4bb33960432735e4d",
10+
"GUID:6ca210c6fc8b79d4292f3cdb1061c73e",
11+
"GUID:a442b1f2ca35f854f80dff74b166d9c0",
12+
"GUID:16fde9420ef50f34db61e711e19381dd",
13+
"GUID:5540e30183c82e84b954c033c388e06c",
14+
"GUID:fe25561d224ed4743af4c60938a59d0b"
15+
],
16+
"includePlatforms": [],
17+
"excludePlatforms": [],
18+
"allowUnsafeCode": false,
19+
"overrideReferences": false,
20+
"precompiledReferences": [],
21+
"autoReferenced": true,
22+
"defineConstraints": [],
23+
"versionDefines": [],
24+
"noEngineReferences": false
25+
}

Packages/UGF.Module.Authentication/Runtime/UGF.Module.Authentication.Runtime.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/UGF.Module.Authentication/Runtime/Unity.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Threading.Tasks;
2+
using UGF.Application.Runtime;
3+
using UGF.Module.Services.Runtime;
4+
using UGF.Module.Services.Runtime.Unity;
5+
using Unity.Services.Authentication;
6+
using Unity.Services.Core;
7+
8+
namespace UGF.Module.Authentication.Runtime.Unity
9+
{
10+
public class AuthenticationUnityModule : AuthenticationModule<AuthenticationUnityModuleDescription>
11+
{
12+
public IAuthenticationService Service { get { return AuthenticationService.Instance; } }
13+
14+
protected ServicesUnityModule ServicesUnityModule { get; }
15+
16+
public AuthenticationUnityModule(AuthenticationUnityModuleDescription description, IApplication application) : base(description, application)
17+
{
18+
ServicesUnityModule = (ServicesUnityModule)Application.GetModule<IServicesModule>();
19+
}
20+
21+
protected override void OnInitialize()
22+
{
23+
base.OnInitialize();
24+
25+
ServicesUnityModule.ConfiguringOptions += OnConfiguringOptions;
26+
}
27+
28+
protected override async Task OnInitializeAsync()
29+
{
30+
await base.OnInitializeAsync();
31+
32+
if (Description.SignAnonymouslyOnInitializeAsync)
33+
{
34+
var options = new SignInOptions
35+
{
36+
CreateAccount = Description.SignAnonymousDescription.CreateAccount
37+
};
38+
39+
await Service.SignInAnonymouslyAsync(options);
40+
}
41+
}
42+
43+
protected override void OnUninitialize()
44+
{
45+
base.OnUninitialize();
46+
47+
ServicesUnityModule.ConfiguringOptions -= OnConfiguringOptions;
48+
}
49+
50+
protected override bool OnIsSigned()
51+
{
52+
return Service.IsSignedIn;
53+
}
54+
55+
protected override void OnSignOut()
56+
{
57+
Service.SignOut(Description.ClearCredentialsOnSignOut);
58+
}
59+
60+
private void OnConfiguringOptions(InitializationOptions options)
61+
{
62+
if (Description.HasProfile)
63+
{
64+
options.SetProfile(Description.Profile);
65+
}
66+
}
67+
}
68+
}

Packages/UGF.Module.Authentication/Runtime/Unity/AuthenticationUnityModule.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using UGF.Application.Runtime;
3+
using UnityEngine;
4+
5+
namespace UGF.Module.Authentication.Runtime.Unity
6+
{
7+
[CreateAssetMenu(menuName = "Unity Game Framework/Authentication/Authentication Unity Module", order = 2000)]
8+
public class AuthenticationUnityModuleAsset : ApplicationModuleAsset<AuthenticationUnityModule, AuthenticationUnityModuleDescription>
9+
{
10+
[SerializeField] private bool m_signAnonymouslyOnInitializeAsync;
11+
[SerializeField] private AuthenticationUnitySignAnonymousData m_signAnonymousSettings;
12+
[SerializeField] private bool m_clearCredentialsOnSignOut;
13+
[SerializeField] private string m_profile;
14+
15+
public bool SignAnonymouslyOnInitializeAsync { get { return m_signAnonymouslyOnInitializeAsync; } set { m_signAnonymouslyOnInitializeAsync = value; } }
16+
public AuthenticationUnitySignAnonymousData SignAnonymousSettings { get { return m_signAnonymousSettings; } set { m_signAnonymousSettings = value; } }
17+
public bool ClearCredentialsOnSignOut { get { return m_clearCredentialsOnSignOut; } set { m_clearCredentialsOnSignOut = value; } }
18+
public string Profile { get { return m_profile; } set { m_profile = value; } }
19+
20+
[Serializable]
21+
public struct AuthenticationUnitySignAnonymousData
22+
{
23+
[SerializeField] private bool m_createAccount;
24+
25+
public bool CreateAccount { get { return m_createAccount; } set { m_createAccount = value; } }
26+
27+
public AuthenticationUnitySignAnonymousDescription GetDescription()
28+
{
29+
return new AuthenticationUnitySignAnonymousDescription(m_createAccount);
30+
}
31+
}
32+
33+
protected override IApplicationModuleDescription OnBuildDescription()
34+
{
35+
return new AuthenticationUnityModuleDescription(
36+
typeof(IAuthenticationModule),
37+
m_signAnonymouslyOnInitializeAsync,
38+
m_signAnonymousSettings.GetDescription(),
39+
m_clearCredentialsOnSignOut,
40+
m_profile
41+
);
42+
}
43+
44+
protected override AuthenticationUnityModule OnBuild(AuthenticationUnityModuleDescription description, IApplication application)
45+
{
46+
return new AuthenticationUnityModule(description, application);
47+
}
48+
}
49+
}

Packages/UGF.Module.Authentication/Runtime/Unity/AuthenticationUnityModuleAsset.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using UGF.Application.Runtime;
3+
4+
namespace UGF.Module.Authentication.Runtime.Unity
5+
{
6+
public class AuthenticationUnityModuleDescription : ApplicationModuleDescription
7+
{
8+
public bool SignAnonymouslyOnInitializeAsync { get; }
9+
public AuthenticationUnitySignAnonymousDescription SignAnonymousDescription { get; }
10+
public bool ClearCredentialsOnSignOut { get; }
11+
public string Profile { get { return HasProfile ? m_profile : throw new ArgumentException("Value not specified."); } }
12+
public bool HasProfile { get { return !string.IsNullOrEmpty(m_profile); } }
13+
14+
private readonly string m_profile;
15+
16+
public AuthenticationUnityModuleDescription(
17+
Type registerType,
18+
bool signAnonymouslyOnInitializeAsync,
19+
AuthenticationUnitySignAnonymousDescription signAnonymousDescription,
20+
bool clearCredentialsOnSignOut,
21+
string profile)
22+
{
23+
RegisterType = registerType ?? throw new ArgumentNullException(nameof(registerType));
24+
SignAnonymouslyOnInitializeAsync = signAnonymouslyOnInitializeAsync;
25+
SignAnonymousDescription = signAnonymousDescription ?? throw new ArgumentNullException(nameof(signAnonymousDescription));
26+
ClearCredentialsOnSignOut = clearCredentialsOnSignOut;
27+
m_profile = profile;
28+
}
29+
}
30+
}

Packages/UGF.Module.Authentication/Runtime/Unity/AuthenticationUnityModuleDescription.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UGF.Description.Runtime;
2+
3+
namespace UGF.Module.Authentication.Runtime.Unity
4+
{
5+
public class AuthenticationUnitySignAnonymousDescription : DescriptionBase
6+
{
7+
public bool CreateAccount { get; }
8+
9+
public AuthenticationUnitySignAnonymousDescription(bool createAccount)
10+
{
11+
CreateAccount = createAccount;
12+
}
13+
}
14+
}

Packages/UGF.Module.Authentication/Runtime/Unity/AuthenticationUnitySignAnonymousDescription.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)